Skip to content

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE fails if the length of the argument is 10 #2280

@mzyy94

Description

@mzyy94

What is the issue you have?

A compilation error occurred while calling NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE and NLOHMANN_DEFINE_TYPE_INTRUSIVE with 9 or 10 members.
It' caused by wrong definition of NLOHMANN_JSON_PASTE10 and NLOHMAN_JSON_PASTE11 implemented by #2225

#define NLOHMANN_JSON_PASTE10(func, v1, v2, v3, v4, v5, v6, v7, v8, v9) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE8(func, v2, v3, v4, v5, v6, v7, v8, v9)
#define NLOHMANN_JSON_PASTE11(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE8(func, v2, v3, v4, v5, v6, v7, v8, v9, v10)

Please describe the steps to reproduce the issue.

  1. Define a struct or class with 9 or 10 members.
  2. Call NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE or NLOHMANN_DEFINE_TYPE_INTRUSIVE with them like shown in this example.
  3. Compile the code.

Can you provide a small but working code example?

#include "single_include/nlohmann/json.hpp"

namespace shopping {
struct customer {
    unsigned int postcode;
    std::string address1;
    std::string address2;
    std::string city;
    std::string country;
    unsigned int telephone;

    std::string name;
    unsigned int age;
    std::string sex;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(customer, postcode, address1, address2, city, country, telephone, name, age, sex);
} // namespace shopping

int main() {}

What is the expected behavior?

Compilation fails.

test.cpp:16:113: error: too many arguments provided to function-like macro invocation
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(customer, postcode, address1, address2, city, country, telephone, name, age, sex);
                                                                                                                ^
./single_include/nlohmann/json.hpp:2166:9: note: macro 'NLOHMANN_JSON_PASTE8' defined here
#define NLOHMANN_JSON_PASTE8(func,  v1, v2, v3, v4, v5, v6, v7)              NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE7(func, v2, v3, v4, v5, v6, v7)
        ^
test.cpp:16:1: error: use of undeclared identifier 'NLOHMANN_JSON_PASTE8'
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(customer, postcode, address1, address2, city, country, telephone, name, age, sex);
^
./single_include/nlohmann/json.hpp:2189:75: note: expanded from macro 'NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE'
    void to_json(nlohmann::json& j, const Type& t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
                                                                          ^
./single_include/nlohmann/json.hpp:2157:9: note: expanded from macro 'NLOHMANN_JSON_PASTE'
        NLOHMANN_JSON_PASTE10, NLOHMANN_JSON_PASTE9, NLOHMANN_JSON_PASTE8, NLOHMANN_JSON_PASTE7, \
        ^
test.cpp:16:113: error: too many arguments provided to function-like macro invocation
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(customer, postcode, address1, address2, city, country, telephone, name, age, sex);
                                                                                                                ^
./single_include/nlohmann/json.hpp:2166:9: note: macro 'NLOHMANN_JSON_PASTE8' defined here
#define NLOHMANN_JSON_PASTE8(func,  v1, v2, v3, v4, v5, v6, v7)              NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE7(func, v2, v3, v4, v5, v6, v7)
        ^
test.cpp:16:1: error: use of undeclared identifier 'NLOHMANN_JSON_PASTE8'
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(customer, postcode, address1, address2, city, country, telephone, name, age, sex);
^
./single_include/nlohmann/json.hpp:2190:77: note: expanded from macro 'NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE'
    void from_json(const nlohmann::json& j, Type& t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
                                                                            ^
./single_include/nlohmann/json.hpp:2157:9: note: expanded from macro 'NLOHMANN_JSON_PASTE'
        NLOHMANN_JSON_PASTE10, NLOHMANN_JSON_PASTE9, NLOHMANN_JSON_PASTE8, NLOHMANN_JSON_PASTE7, \
        ^
4 errors generated.

And what is the actual behavior instead?

The compilation is successful.

Which compiler and operating system are you using?

  • Compiler: Apple clang version 11.0.3 (clang-1103.0.32.62)
  • Operating system: macOS Catalina 10.15.5(19F101)

Which version of the library did you use?

  • latest release version 3.7.3
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no - please copy/paste the error message below

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions