Friday, July 7, 2017

C++11: Initializer Lists

An initializer-list constructor is a constructor that has a single argument of type: std::initializer-list.


C++11: Initializer Lists

Initializer lists used to be limited to arrays.  Now that can be used with any type.

C++11: range-for

The following are examples of the ‘range-for’ statement:
for (auto element : vector) {}
for (auto& element : vector) {}
for (const auto element : vector) {}
for (const auto& element : vector) {}

The first returns copies of the vector elements in the loop, and the second returns references to the vector elements in the loop. The third and fourth examples are const versions of the first two.


C++11: range-for

The ‘range-for’ statement lets you iterate through a container that provides begin() and end() iterators.

C++11: decltype

decltype(N) can be used as a type specifier. The type used is the type of N. For example: int i; decltype(i) j; declares j to be of type int.


C++11: auto

‘auto ‘ can  be used for the type. The type will be inferred from the initializer. The old meaning of ‘auto’ as a storage specifier (stack variable) was made illegal.

C++03

C++03 replaced C++98. It consisted mostly of bug fixes. The most important bug fixed was the specification that Vector elements where stored contiguous in memory.  Most compilers already enforced this. This requirement allowed compatibility between C++ vectors and C arrays.