Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use VA_OPT to visit empty structs as well #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

1-Harshit
Copy link

As discussed earlier @cbeck88, raising a patch here as well.

TL;DR
The current setup doesn't work with empty struct. Patched solution:
GCC godbolt: https://godbolt.org/z/44T1ndTbh
MSVC godbolt: https://godbolt.org/z/ozxfYa7PW

Full explanation:
There's a catch to VISIT_STRUCT_PP_MAP. Ideally, it should do nothing if no arguments are provided, but the build crashes for such cases. The underlying issue is:

  • VISIT_STRUCT_PP_MAP(f) expands to VISIT_STRUCT_APPLY_F_(VISIT_STRUCT_CONCAT(VISIT_STRUCT_APPLYF, VISIT_STRUCT_PP_NARG()), f,)
  • Which in turn should become VISIT_STRUCT_APPLYF0(f) [Note that this 0 is the resolution of macro VISIT_STRUCT_PP_NARG()]
  • But it expands to VISIT_STRUCT_APPLYF1(f,)

This, in turn, causes a compilation issue for my use case. I investigated the issue, and here is the problem:

  • For VISIT_STRUCT_PP_NARG(ONE), the macro expands as VISIT_STRUCT_PP_ARG_N(ONE, 69, 68, ...) -> Where the Nth term becomes 1, and we get 1 [Correct behaviour]
  • For VISIT_STRUCT_PP_NARG() [no args] the macro expands as VISIT_STRUCT_PP_ARG_N(, 69, 68, ...) -> Where also the Nth term becomes 1, and we get 1 [Incorrect behaviour]

This happens as the empty field before the comma is considered an argument to the macro. ++23 introduces a solution for such cases with the reserved macro VA_OPT.

Now VISIT_STRUCT_PP_NARG() resolve to 0 in cases where no arguments are provided. I’ve included two Godbolt links comparing the original and patched versions for your reference original Godbolt vs. patched Godbolt.

Copy link
Owner

@cbeck88 cbeck88 left a comment

Choose a reason for hiding this comment

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

it looks good to me, thank you!

@1-Harshit
Copy link
Author

1-Harshit commented Jan 1, 2025

@cbeck88 I don't have write access to the repository to merge these changes
image

@1-Harshit
Copy link
Author

@cbeck88 should we update the VISIT_STRUCT_VERSION_PATCH to 1 ?

@cbeck88
Copy link
Owner

cbeck88 commented Jan 3, 2025

@cbeck88 should we update the VISIT_STRUCT_VERSION_PATCH to 1 ?

Sure that seems good

@cbeck88
Copy link
Owner

cbeck88 commented Jan 3, 2025

@cbeck88 I don't have write access to the repository to merge these changes image

Sorry, I thought it would let you merge with approval. I’ll merge shortly

@1-Harshit
Copy link
Author

@cbeck88 update patch version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants