Giter Site home page Giter Site logo

value and uniform initialization about json HOT 5 CLOSED

taocpp avatar taocpp commented on May 14, 2024
value and uniform initialization

from json.

Comments (5)

d-frey avatar d-frey commented on May 14, 2024 1

@ColinH Yes, it should be in the documentation. I'll leave the issue open as a reminder.

from json.

d-frey avatar d-frey commented on May 14, 2024

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.

ColinH avatar ColinH commented on May 14, 2024

(@d-frey This should be somewhere in the documentation.)

from json.

matbech avatar matbech commented on May 14, 2024

@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.

d-frey avatar d-frey commented on May 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.