Qwertypal
Qwertypal

Reputation: 189

C++ new syntax, pls explain

What the syntax is called and what does it do? For which c++ standard it is?

shared_ptr<int> p{new int{10}};

I am confused about the first set of curly brackets {}. I suppose, the second set is creating a temporary object of 10 elements?

Upvotes: 1

Views: 157

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

What the syntax is called, and what does it do?

This syntax is called uniform initialization or list initialization. It does many different things explained here. In this particular case the construct initializes the shared pointer with a plain pointer to an integer, and sets the newly allocated integer to ten (demo).

The effect is the same as in the code snippet below:

int *tmp = new int;
*tmp = 10;
shared_ptr p(tmp);

For which c++ standard it is?

This syntax was introduced in C++11.

Upvotes: 5

Related Questions