nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.3k stars 6.58k forks source link

Simple example with nlohmann::ordered_json doesn't compile #4282

Closed patrikhuber closed 5 months ago

patrikhuber commented 5 months ago

Description

Hi,

I wanted to write a json file where the order in the json file is the same as the order of the member variables of my struct - so after some googling, I found nlohmann::ordered_json, and wanted to try it out. However a simple example doesn't seem to work (I'm on the latest nlohmann::json version).

My apologies if I'm just overlooking something or making some stupid mistake.

Reproduction steps

See code.

Expected vs. actual results

I would have expected the code to compile.

Minimal code example

#include "nlohmann/json.hpp"

struct MyStruct
{
    bool n;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(MyStruct, n)

int main()
{
    MyStruct s;
    nlohmann::json j = s; // compiles
    nlohmann::ordered_json k = s; // compiler error
}

### Error messages

```Shell
See https://godbolt.org/z/6bEvr5ezn. I'm on MSVC (latest) but compiler explorer says the error occurs on gcc-13.2 too.

Compiler and operating system

MSVC (latest) / gcc-13.2

Library version

latest

Validation

nlohmann commented 5 months ago

See https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/ implementation limits.

patrikhuber commented 5 months ago

Ah thank you for the docs link, sorry I haven't seen that. I guess we can close this then!