Why is std::make_unique not implemented using list initialization?

In C++20, this will compile: std::make_unique<point>(1, 2); due to the new rule allowing initializing aggregates from a parenthesized list of values. In C++17, you can just do: std::unique_ptr<point>(new point{1, 2}); That won’t work with make_shared though. So you can also just create a factory (forwarding left as an exercise): template <typename… Args> struct braced_init { … Read more

Why ={} initialization doesn’t work for tuple?

In addition to Praetorian’s correct answer (which I’ve upvoted), I wanted to add a little more information… Post-C++14, the standard has been changed to allow: tuple<int, int> t2={1, 2}; to compile and have the expected semantics. The proposal that does this is N4387. This will also allow constructs such as: tuple<int, int> foo() { return … Read more

How does the number of braces affect uniform initialization?

Overload resolution is fun like this. {a} has exact match rank for initializing (a temporary for) the const A& parameter, which outcompetes the user-defined conversion B(const A&) as a realization of {a}. This rule was added in C++14 to resolve ambiguities in list-initialization (along with adjustments for aggregates). Note that the notional temporary is never … Read more

What do braces on the left-hand side of a variable declaration mean, such as in T {x} = y?

It’s not a declaration. It’s an assignment to a temporary. In std::unique_ptr<int> {p} = std::make_unique<int>(1);, std::unique_ptr<int> {p} creates a unique_ptr temporary that takes ownership of the object p points to, then std::make_unique<int>(1) is assigned to that temporary, which causes the object p points to to be deleted and the temporary to take ownership of the … Read more

How to initialize an array member in a member initializer list

How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible? Yes. It’s using a struct that contains an array. You say you already know about that, but then I don’t understand the question. That way, you do initialize … Read more

lifetime of a std::initializer_list return value

std::initializer_list is not a container, don’t use it to pass values around and expect them to persist DR 1290 changed the wording, you should also be aware of 1565 and 1599 which aren’t ready yet. Then the return value’s array should also survive into the calling function, and it should be possible to preserve it … Read more

Why does the standard differentiate between direct-list-initialization and copy-list-initialization?

Because they don’t do the exact same thing. As stated in 13.3.1.7 [over.match.list]: In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed. In short, you can only use implicit conversion in copy-list-initialization contexts. This was explicitly added to make uniform initialization not, um, uniform. Yeah, I know how stupid that sounds, but … Read more

Does the equal sign make a difference in brace initialization? eg. ‘T a = {}’ vs ‘T a{}’

The only significant difference I know is in the treatment of explicit constructors: struct foo { explicit foo(int); }; foo f0 {42}; // OK foo f1 = {42}; // not allowed This is similar to the “traditional” initialization: foo f0 (42); // OK foo f1 = 42; // not allowed See [over.match.list]/1. Apart from that, … Read more

Why does this code compile (C++11) without a type mismatch error?

You’re not calling vector‘s constructor that takes an initializer_list<char>. That constructor is not viable because, as you said, you’re not passing a list of chars. But vector also has a constructor that takes iterators to a range of elements. template< class InputIt > vector( InputIt first, InputIt last, const Allocator& alloc = Allocator() ); Unfortunately, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)