-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
NLOHMANN_DEFINE_TYPE_* fails with zero members #4041
Comments
In case someone was interested in how to proceed with fixing this issue: This bug is caused by the way the macro functional meta programming is implemented for this project and by a standard behaviour of C preprocessor's Due to this property of So, for instance, See json/include/nlohmann/detail/macro_scope.hpp Line 406 in a0c1318
There are several ways how to tackle with this problem:
|
Alternatively, just add another macro, eg. |
It can be easily fixed with #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
friend void to_json(nlohmann::json& __VA_OPT__(nlohmann_json_j), const Type& __VA_OPT__(nlohmann_json_t)) { __VA_OPT__(NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__))) } \
friend void from_json(const nlohmann::json& __VA_OPT__(nlohmann_json_j), Type& __VA_OPT__(nlohmann_json_t)) { __VA_OPT__(NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__))) } But #ifndef JSON_HAS_CPP_20
#define __VA_OPT__(x) x
#endif but that still won't fix the bug for the earlier versions. |
Should be fixed by #4323. |
Description
I found it annoying when working with multiple tiny polymorphic classes, trying to provide identical interface for each of them, that I can't use the macro for a class that has no members to (de)serialize.
Reproduction steps
Use (probably) any of the
NLOHMANN_DEFINE_TYPE_*
macros with only first argument and no members.F.e.
NLOHMANN_DEFINE_TYPE_INTRUSIVE(MyClass)
Expected vs. actual results
Expected:
Actual:
Minimal code example
Error messages
Compiler and operating system
Microsoft Visual C++ 2022
Library version
3.11.2
Validation
develop
branch is used.The text was updated successfully, but these errors were encountered: