0

I have no experience with C++, so this might be a stupid question, but I've tried googling for this (which is hard, I'm not sure how google handles parens braces etc in its search) and even tried some vector and struct tutorials, but I can't figure it out.

I'm running into a line of code which I can't understand in a source I've downloaded, what does this line mean?

std::vector<node> nodes = std::vector<node>{{node{}}};

node is a struct, defined as:

struct node {
    enum class type : uint16_t {
        none = 0,
        integer = 1,
        real = 2,
        string = 3,
        vector = 4,
        bitmap = 5,
        audio = 6,
        uol = 7
    };
    uint32_t name = 0;
    uint32_t children = 0;
    uint16_t num = 0;
    type data_type = type::none;
    union _data {
        int64_t integer = 0;
        double real;
        uint32_t string;
        int32_t vector[2];
        struct _bitmap {
            uint32_t id;
            uint16_t width;
            uint16_t height;
        } bitmap;
        struct _audio {
            uint32_t id;
            uint32_t length;
        } audio;
    } data;
};

It's giving me a compiler error complaining that there is no viable conversion from node to uint32_t.

The only part of the line I don't understand is the nested curly braces at the end, so I'm assuming that it's where the problem is. But what is it doing?

I thought maybe you don't need parentheses to call a constructor if it has only one argument, and its passing a list with node{} within a list, e.g.,

std::vector<node> nodes = std::vector<node>({{node{}}});

but then why is it trying to convert it to uint32_t?? I am so very confused. If anyone can shed some light on this I'd be very grateful.

kumowoon1025
  • 201
  • 1
  • 9
  • For more information on dark corners of the uniform initialization and type conversions check [this post](http://scottmeyers.blogspot.fr/2015/09/thoughts-on-vagaries-of-c-initialization.html) by Scott Meyers (and also Item 7 of his [new book](http://www.amazon.com/Effective-Modern-Specific-Ways-Improve/dp/1491903996)). – Ivan Aksamentov - Drop Jan 02 '16 at 03:10
  • Wow, thanks, I would never have found uniform initialization on my own. – kumowoon1025 Jan 02 '16 at 04:13
  • So what I understand so far is that it should initialize a `vector` with a vector that contains a `node` struct, so I still don't understand why it is trying to convert `node` to an unsigned int. I must not be understanding this still, could you help me with exactly what I'm missing though? – kumowoon1025 Jan 02 '16 at 04:16

0 Answers0