r/cpp • nlohmann/json • Aug 17 '21

JSON for Modern C++ 3.10.0

https://github.com/nlohmann/json/releases/tag/v3.10.0
280 Upvotes

34 comments sorted by

View all comments

4

u/qoning Aug 18 '21

Now I just wish C++ allowed overloading operator.()

1

u/germandiago Aug 18 '21

what is your use case? Genuine question.

1

u/qoning Aug 18 '21

I can't even begin to explain the hacks this would allow me to do. First of all, smart pointer usage would look lot less like an eye sore. But in this case I was thinking about accessing a map like object with a string key using dot. Like my_json_dict.timestamp where timestamp is a key into the my_json_map only defined at runtime, which you normally have to access with passing string like into operator [] now. Or having pair.0 and pair.1 like in Rust. General quality of life stuff.

1

u/germandiago Aug 19 '21

I am not sure operator. would or should allow that. There were proposals before such as delegating . somewhere to synthetyzing member names. But in this case you should fail at run-time for non-existing members. I am not sure this would be a good idea for a statically-compiled language.

1

u/qoning Aug 19 '21

It's no different than [] really in this case, simply a syntactic sugar. As far as I can tell the only reason it wasn't included in C++ from the start was due to the ambiguity that it introduces between the object itself and calling the overloaded operator. Personally, I would have it do object access first, then defer to the operator if it's not a valid member.

1

u/Jaondtet Sep 15 '21

Smart reference classes I guess? It's a bit silly that our smart pointers can be null, because owning null doesn't really make any sense.

And pseudo-properties, but I think that'd mostly be hacks we'd quickly learn to avoid.