3

So far I've found following uses of list initialization (a.k.a. uniform initialization).

1) Before Introduction was list initialization feature

int a=3.3f; // ouch fractional part is automatically truncated 

But in C++11

int a{3.3f}; // compiler error no implicit narrowing conversion allowed 

2) Dynamic array elements can be initialized statically. See for example this program is invalid in C++03 but valid since C++11:

#include <iostream> int main() { int* p=new int[3]{3,4,5}; for(int i=0;i<3;i++) std::cout<<p[i]<<' '; delete[] p; } 

3) It solves most vexing parse problem

It would be better if you tell me other advantages of list initialization. Are there still any advantages of list initialization except above 3?

Your answer is highly appreciated.

7
  • 2
    You stacked-crooked.com link is broken, you are just linking directly to the site not to your example. Commented Sep 8, 2015 at 17:12
  • @mattnewport: fixed it. See updated question !!! Commented Sep 8, 2015 at 17:14
  • Note a narrowing conversion is ill-formed which requires a diagnostic not an error. Your question seems to imply is requires an error. Commented Sep 8, 2015 at 17:21
  • @ShafikYaghmour consistent with the C++ Standard, which in examples uses "error: ..." when documenting ill-formed code Commented Sep 8, 2015 at 17:44
  • 1
    @ShafikYaghmour: As far as I know, the standard does not explicitly mention the idea of compiler errors versus compiler warnings. Which things are errors vs. warnings is left up to the implementation, and usually configurable by the user. The compiler is never required not to emit code in some case, it is only required to emit code conforming to the spec for well-formed programs, and to emit diagnostics in some other cases. And in case of undefined behavior it can emit whatever it wants. Commented Sep 8, 2015 at 18:37

2 Answers 2

4

A significant advantage you don't mention is its usefulness in template metaprogramming, where you can now compute something using templates, and then, in a constexpr function, expand some template data structure and store the results in an array.

See for instance here: Populate An Array Using Constexpr at Compile-time

In the code:

template<unsigned... Is> constexpr Table MagicFunction(seq<Is...>){ return {{ whichCategory(Is)... }}; } 

I don't think there was any way to do anything like that prior to C++11.

Sign up to request clarification or add additional context in comments.

Comments

4

I'm not sure if you consider it a separate feature but the same syntax is also used to overload constructors on std::initializer_list which allows you to initialize STL containers directly:

std::map<std::string, std::string> m{{"foo", "bar"}, {"apple", "pear"}}; std::cout << m["foo"] << std::endl; 

1 Comment

With the slight disadvantage that std::vector<int> v(1, 2); is different from std::vector<int> v{1, 2};.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.