NLOHMANN_DEFINE_TYPE_* fails with zero members
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 50.6k
- Forks
- 7.5k
- Avg merge
- 4d 17h
- Merged PRs (30d)
- 58
Description
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:
friend void to_json(nlohmann::json&, const MyClass&) {}
friend void from_json(const nlohmann::json&, MyClass&) {}
Actual:
friend void to_json(nlohmann::json& nlohmann_json_j, const MyClass& nlohmann_json_t) {
nlohmann_json_j[] = nlohmann_json_t.;
}
friend void from_json(const nlohmann::json& nlohmann_json_j, MyClass& nlohmann_json_t) {
nlohmann_json_j.at().get_to(nlohmann_json_t.);
}
Minimal code example
class MyClass
{
NLOHMANN_DEFINE_TYPE_INTRUSIVE(MyClass);
};
Error messages
Error C2661 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::at': no overloaded function takes 0 arguments
Error C2661 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::at': no overloaded function takes 0 arguments
Error C2661 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::at': no overloaded function takes 0 arguments
Error C2661 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::at': no overloaded function takes 0 arguments
Error C2059 syntax error: ')'
Error C2059 syntax error: ')'
Error C2059 syntax error: ')'
Error C2059 syntax error: ')'
Error C2059 syntax error: ']'
Error C2059 syntax error: ']'
Error C2059 syntax error: ']'
Error C2059 syntax error: ']'
Compiler and operating system
Microsoft Visual C++ 2022
Library version
3.11.2
Validation
- The bug also occurs if the latest version from the
developbranch is used. - I can successfully compile and run the unit tests.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Search the repository for the definitions of NLOHMANN_DEFINE_TYPE_INTRUSIVE and the related NLOHMANN_DEFINE_TYPE_* macros, then reproduce the empty-class example with C++ and the reported compiler. Done means the zero-member form compiles and produces usable empty serialization functions, while existing member forms continue to pass the unit tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100