static_assert on initializer_list::size()

“Initializer lists” are just horrible kludges. Don’t: #include <initializer_list> template<typename T> void Dont(std::initializer_list<T> list) { // Bad! static_assert(list.size() == 3, “Exactly three elements are required.”); } void Test() { Dont({1,2,3}); } Do: template<typename T, std::size_t N> void Do(const T(&list)[N]) { // Good! static_assert(N == 3, “Exactly three elements are required.”); } void Test() { Do({1,2,3}); … Read more

How can I make a constructor which lets me construct with a braced-init-list?

It can only be done for aggregates (arrays and certain classes. Contrary to popular belief, this works for many nonpods too). Writing a constructor that takes them is not possible. Since you tagged it as “C++0x”, then this is possible though. The magic words is “initializer-list constructor”. This goes like Phenotype(std::initializer_list<uint8> c) { assert(c.size() <= … Read more

Why use variadic arguments now when initializer lists are available?

If by variadic arguments you mean the ellipses (as in void foo(…)), then those are made more or less obsolete by variadic templates rather than by initializer lists – there still could be some use cases for the ellipses when working with SFINAE to implement (for instance) type traits, or for C compatibility, but I … Read more

Why could const member be initialized twice?

It’s not initialized twice; the default member initializer is just ignored. So for A a(555);, a.k is initialized as 555. If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored. From the standard, [class.base.init]/10: If a given non-static data member … Read more

Calling initializer_list constructor via make_unique/make_shared

std::make_unique is a function template which deduces the argument types which are passed to the object constructor. Unfortunately, braced lists are not deducible (with an exception for auto declarations), and so you cannot instantiate the function template when that missing parameter type. You can either not use std::make_unique, but please don’t go that route – … Read more

How to construct std::array object with initializer list? [duplicate]

An std::array<> has no constructor that takes an std::initializer_list<> (initializer list constructor) and there is no special language support for what it may mean to pass a std::initializer_list<> to a class’ constructors such that that may work. So that fails. For it to work, your derived class needs to catch all elements and then forward … Read more

What could go wrong if copy-list-initialization allowed explicit constructors?

Conceptually copy-list-initialization is the conversion of a compound value to a destination type. The paper that proposed wording and explained rationale already considered the term “copy” in “copy list initialization” unfortunate, since it doesn’t really convey the actual rationale behind it. But it is kept for compatibility with existing wording. A {10, 20} pair/tuple value … Read more

initializer_list and template type deduction

Your first line printme({‘a’, ‘b’, ‘c’}) is illegal because the template argument T could not be inferred. If you explicitly specify the template argument it will work, e.g. printme<vector<char>>({‘a’, ‘b’, ‘c’}) or printme<initializer_list<char>>({‘a’, ‘b’, ‘c’}). The other ones you listed are legal because the argument has a well-defined type, so the template argument T can … Read more