Tomáš Zato
Tomáš Zato

Reputation: 53338

What do curly brackets enclosing value literals evalue to in function call?

I recently received an answer that uses the following syntax:

map.left.insert({165, "NODE_A"});

It works. But I have totally no idea what does this syntax mean.

It could be some kind of anonymous struct initialization, or maybe a way to avoid including some struct's header file. But these are just ideas, I found nothing about it on google.

Upvotes: 0

Views: 2081

Answers (2)

learnvst
learnvst

Reputation: 16193

It is an initialiser list

Just like with a function that takes a vector of ints can be called with an initializer list of values, rather than making a vector and then pushing stuff into it and then handing it off to the function, i.e.

someFunc({2,3,4,5,6});

Looks like the same deal for your question in the previous answer. You can go even more crazy if the function that was being called accepted a vector of paired values using syntax like the following . .

someFunc({
       {165, "NODE_A"},
       {167, "NODE_B"},
       // etc . . .
    });

It is just a C++11 nicety.

Upvotes: -1

Amadeus
Amadeus

Reputation: 10685

It is a new feature added in c++11. It allows you to instantiated a object just passing its constructor arguments.

class Foo
{
public:
    Foo(int a, char b) {}
};

void func(Foo a)
{}

int main()
{
    func({2, 'c'});
}

Upvotes: 2

Related Questions