1

One main and obvious meaning, found in the standard library - "initializing a collection with a list of its elements":

std::vector<int> v = {1, 2, 3};

Another meaning can be found behind the link on std::bitset below - "the single value is assembled from elements of initializer_list".

The third example in the standard library is std::piecewise_constant_distribution, but I hesitate to say what semantic it has, not exactly a collection of elements.

What are other use cases for std::initializer_list constructors? If possible, with examples from real code.

It's actually a question about class design.
Because of some pecularities of list-initialization adding an std::initializer_list constructor to already existing class can easily an surprisingly be a breaking change, so when writing a new class you should always know in advance if it will ever need an std::initializer_list constructor.
So, I'm trying to simulate the ability to see the future by writing out use cases for std::initializer_list constructors.

The primary question is: How do I determine that my class will probably have (not surprising for users) std::initializer_list constructors in the future to write correct non-std::initializer_list constructors now?

Community
  • 1
  • 1
user2665887
  • 863
  • 5
  • 19

2 Answers2

1

I am actually using this technique in my current project. I have a class "BaseMenu" and it needs std::vector initialized in it's constructor, so it has std::vector as it's argument. Then, I have MainMenu which inherits from BaseMenu and I just tell BaseMenu what I want my MainMenu look like:

BaseMenu(std::vector<std::string>);

MainMenu::MainMenu():
    BaseMenu({{"Play"},
              {"Options"},
              {"About"},
              {"Quit"}})

It is really "comfortable". PS: the model above is simplified, but it should give the sense of usefulness of std::initializer_list

user3496846
  • 1,627
  • 3
  • 16
  • 28
1

I think intializer_list has the following key feature:

  1. It encapsulate the initialization concept in a class.
  2. This mechanism also provides the type safety and avoids any conversions that may cause information loss.
  3. Its provides the only const_iterator type interface so that it can be optimize for reading and no body should be able to update/write into it. This make sense as it is meant to initialize the other object.

For practical example, you may want to refer my blog and ISOCPP information.

EDIT

Possible Advantages of std::initializer_list

//1. Initialization Concept In A class
std::initializer_list<int>  x{1,2,3,4,5};
std::vector<int> v(x.begin(), x.end());




//2.Better Type Safety and avoid narrow conversions scenario
 std::vector<int> v{1, 2, 3.4, 4};

 Compiler Output

 narrowing conversion of ‘3.3999999999999999e+0’ from ‘double’ to ‘int’ inside { }



//3. Provide Const iterator type interface
std::initializer_list<int> ly{1,2,3,4,5};
auto itrs = ly.begin();
int* x = itrs;

Compiler Output

error: invalid conversion from ‘const int*’ to ‘int*’ 
Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48
  • I probably have some problems with my wording (or my English), because both you and user3496846 answered some questions which I didn't asked – user2665887 Apr 08 '14 at 17:46
  • @user2665887:I think you wanted to understand the intializer_list use cases/advantage with practical examples.I have updated my post with examples. Hopefully you may find it useful. – Mantosh Kumar Apr 08 '14 at 18:10
  • All do respect, but you asked a very open ended question so it should be of no surprise when you get a wide variety of answers! Your best bet is to search the internet for examples, or find a good C++ book with C++11 info. If you ask such a question, it is hard to predict what answers you will get. – shawn1874 Apr 08 '14 at 18:13
  • Well, I edited the question a bit, not sure if it helps – user2665887 Apr 08 '14 at 19:25
  • @user2665887: Well I would suggest you to see the standard library classes and follow the same. In general container type classes should have one constructor which can be initialized using initializer list(like array,vector,list,map......). And for non- container type do not write such constructor(iostream....)..This is kind of general rule which normally gets followed in standard so we should also follow the same. – Mantosh Kumar Apr 09 '14 at 01:54