Skip to content

Fix #4041: Support zero-member types in NLOHMANN_DEFINE_TYPE_* macros#5142

Open
suchetindrakanty wants to merge 1 commit into
nlohmann:developfrom
suchetindrakanty:fix-issue-4041
Open

Fix #4041: Support zero-member types in NLOHMANN_DEFINE_TYPE_* macros#5142
suchetindrakanty wants to merge 1 commit into
nlohmann:developfrom
suchetindrakanty:fix-issue-4041

Conversation

@suchetindrakanty

Copy link
Copy Markdown

Fixes #4041

This PR addresses an issue where NLOHMANN_DEFINE_TYPE_* macros fail when used with zero members.

Problem

When VA_ARGS is empty, macro expansion generates invalid code, causing compilation errors.

Solution

  • Wrapped macro expansions with VA_OPT to ensure code is only generated when arguments are present
  • Prevents invalid expansions for zero-member types
  • Maintains existing behavior for non-empty cases

Testing

  • Added test cases for intrusive and non-intrusive macros
  • Covered WITH_DEFAULT and ONLY_SERIALIZE variants
  • Verified empty structs serialize to {}
  • Verified deserialization works correctly

@gregmarr

Copy link
Copy Markdown
Contributor

I suppose this can co-exist with #5099 in that users of older standards need to use the specialized macros, and users of the newer standards can use the current macros.

@github-actions

Copy link
Copy Markdown

🔴 Amalgamation check failed! 🔴

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

@github-actions

Copy link
Copy Markdown

This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions!

@github-actions github-actions Bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 17, 2026
@nlohmann nlohmann added the please rebase Please rebase your branch to origin/develop label Jul 5, 2026

@nlohmann nlohmann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

__VA_OPT__ is a C++20 preprocessor feature (roughly GCC 8+/Clang 12+), used here completely unconditionally with no #if defined(JSON_HAS_CPP_20) or compiler-version check — despite the file already having an established pattern for exactly this (see the filesystem feature-detection guard a few lines above). Since README.md documents support for GCC 4.8–14.2 and Clang 3.4–21.0, and the project's own CI (ci_test_compilers_gcc_old: 4.8/4.9/5/6, ci_test_compilers_clang: 3.4–11) actually builds on those toolchains, every existing non-empty-member use of these 12 macros — not just the new zero-member case — will fail to compile with an "undeclared identifier __VA_OPT__" error on those compilers. The new comment claiming "there is no regression" is only true for the previously-broken zero-member path; it's false for the common, non-empty-member path this PR puts at risk.

@github-actions github-actions Bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

L please rebase Please rebase your branch to origin/develop tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NLOHMANN_DEFINE_TYPE_* fails with zero members

3 participants