Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) initializer-list

 

An initializer-list is on option that can be used, depending on the standard used:

 

 

 

 

 

C++98 initializer list in the C++98 standard

 

initializer list is not supported in C++98.

 

#include <vector>

int main()
{
  std::vector<int> v = {1,2,3,4,5}; //Fails in C++98
}

 

Compiler output:

 

/MyFolder/main.cpp:5: error: in C++98 'v' must be initialized by constructor, not by '{...}'
/MyFolder/main.cpp:5: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
/MyFolder/main.cpp:5: error: deducing from brace-enclosed initializer list requires #include <initializer_list>
/MyFolder/main.cpp:5: error: no matching function for call to 'std::vector<int, std::allocator<int> >::vector(<brace-enclosed initializer list>)'
/usr/include/c++/4.4/bits/stl_vector.h:241: candidates are: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = int, _Alloc = std::allocator<int>]
/usr/include/c++/4.4/bits/stl_vector.h:207:                 std::vector<_Tp, _Alloc>::vector() [with _Tp = int, _Alloc = std::allocator<int>]

 

 

 

 

 

C++11 initializer list in the C++11 standard

 

 

An initializer list is an additional initialization option:

 

#include <vector>

int main()
{
  std::vector<int> v = {1,2,3,4,5};
}

 

 

 

 

 

Advice

 

 

 

 

 

 

References

 

  1. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[8] If a class is a container, give it an initializer-list constructor'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict