Skip to content

fix: support zero-member types in NLOHMANN_DEFINE_TYPE_* macros#5177

Closed
leno23 wants to merge 1 commit into
nlohmann:developfrom
leno23:fix/macro-zero-members-4041
Closed

fix: support zero-member types in NLOHMANN_DEFINE_TYPE_* macros#5177
leno23 wants to merge 1 commit into
nlohmann:developfrom
leno23:fix/macro-zero-members-4041

Conversation

@leno23
Copy link
Copy Markdown

@leno23 leno23 commented May 17, 2026

Summary

NLOHMANN_DEFINE_TYPE_* with no member fields produced invalid to_json / from_json bodies (e.g. nlohmann_json_j[] = nlohmann_json_t.).

Adds NLOHMANN_JSON_PASTE0 and NLOHMANN_JSON_DOUBLE_PASTE0 so empty __VA_ARGS__ expand to no-ops, matching the expected empty function bodies.

Fixes #4041.

Test plan

  • Added unit tests for intrusive and non-intrusive zero-member types in unit-udt_macro.cpp.
  • CI (project test suite).

Made with Cursor

Add NLOHMANN_JSON_PASTE0 and NLOHMANN_JSON_DOUBLE_PASTE0 so empty
member lists expand to no-op serialization code.

Fixes nlohmann#4041.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ca10ccfbc9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +342 to +344
NLOHMANN_JSON_PASTE1, \
NLOHMANN_JSON_PASTE0)(__VA_ARGS__))
#define NLOHMANN_JSON_PASTE0(func)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Route empty member lists to the zero-arg paste branch

Adding NLOHMANN_JSON_PASTE0 here does not make zero-member NLOHMANN_DEFINE_TYPE_* calls work, because the call sites pass NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__); when __VA_ARGS__ is empty this still leaves an empty argument after the comma, so dispatch selects NLOHMANN_JSON_PASTE2 and expands to invalid code like nlohmann_json_t.. In practice, NLOHMANN_DEFINE_TYPE_INTRUSIVE(MyType) still fails to compile, so the new tests introduced in this commit cannot build.

Useful? React with 👍 / 👎.

@github-actions
Copy link
Copy Markdown

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @leno23
Please read and follow the Contribution Guidelines.

@nlohmann
Copy link
Copy Markdown
Owner

@leno23 Can you explain your patch? It seems it is just generated by Cursor, but never even tried to be compiled.

@nlohmann
Copy link
Copy Markdown
Owner

AI slop.

@nlohmann nlohmann closed this May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NLOHMANN_DEFINE_TYPE_* fails with zero members

2 participants