Comments (5)
@ColinH Yes, it should be in the documentation. I'll leave the issue open as a reminder.
from json.
That's the problem with uniform initialization: It's not uniform.
The compiler currently sees it as an initializer list with one element, trying to construct a JSON object with one element. Let's consider a few cases of what you could write:
my_class c; // my_class has a default_key set through the traits
value v1; // uninitialized
value v2{}; // uninitialized (as this is always the default ctor, never our initializer list ctor)
value v3(); // most vexing parse, ignore this case...
value v4({}); // empty object
value v5{{}}; // empty object
value v6 = v0; // copy ctor
value v7( v0 ); // copy ctor
value v8{ v0 }; // does not call copy ctor, but initializer list ctor -> doesn't work
value v9 = c; // convert my_class instance to v9
value v10( c ); // convert my_class instance to v10
// create an object with one key/value pair,
// key is default_key for my_class,
// value is the converted my_class instance:
value v11 = { c };
value v12{ c }; // same as v11.
value v13( { c } ); // same as v11.
value v14{ { c } }; // same as v11.
As you can see, there are quite a few cases to consider and not everything that C++ accepts seems to make sense, e.g. how is v12, v13, and v14 always the same result? I didn't intend this to work, but I have to accept the language as-is.
The case you are looking at is v8. If I want that to work and be the copy ctor, what about value v15{ 42 };
? Should that work as well? Also, consider a specialization of the traits for a value:
namespace tao { namespace json {
template<>
struct traits<value>
{
static const char* default_key;
static void assign( value& v, const value& x )
{
v = x;
}
};
This is currently legal and if you use it, you can write
value v0 = 42;
value v1{ v0 };
and now v1
is an object with one key/value pair where the value is a number (42).
It also allows
value v2{ v0, { "foo", "bar" } };
which will give you
{"json":42,"foo":"bar"}
I don't see how I can support all cases properly, technically it's not really possible to overload for them as needed. If I'm wrong about this, I'd appreciate a concrete suggestion on how you'd handle it and what overloads/changes are needed.
from json.
(@d-frey This should be somewhere in the documentation.)
from json.
@d-frey Thank you for the explanation. Unfortunately, I do not have a good suggestion to really solve this issue. I was hoping you had one :-)
from json.
My suggestion is: Stop using "uniform initialization", I still use ()
whenever I can - only if there is a good reason to use {}
I do. 🙂
from json.
Related Issues (20)
- CMake imported target is not `taocpp::json` anymore HOT 2
- How to write cbor/jaxn/msgpack and other file ? HOT 1
- demangle.hpp build failure for MSVC HOT 1
- How can we improve tao-json performance ? HOT 2
- transforming unsigned to chrono::duration HOT 2
- the conan package at https://conan.io/center/taocpp-json missing to/from_nolhmann.hpp files HOT 2
- Stack-overflow occured when parsing a bad json file HOT 2
- why isn't to/from_nolhmann.hpp files are excluded from contrib folder on release HOT 1
- Binding questions HOT 14
- Issues using 1.0.0-beta.13 with C++20 HOT 5
- About the first stable version of Taojson HOT 1
- looks like `tao::json::value::operator[]` doesn't accept string as a key HOT 3
- simple json program won't compile on raspberry pi bullseye HOT 6
- feature request: std::string_view as first argument to TAO_JSON_BIND_REQUIRED HOT 3
- `rewind_mode::dontcare` not implemented in PEGTL? HOT 1
- Reset Event consumer? HOT 2
- Question: conditionally group bindings in a trait?
- json main cannot be compiled with PEGTL main
- Question: nesting binding::object in one binding trait definition possible/supported? HOT 3
- traits: ability to define what optional means HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json.