The core of C++ language and its library are typically standardized in parallel. By this means, the library can be advantageous for improvements in the language and the language can profit from experiences of library performance. So, as a result, a C++ standard library constantly uses precise language features that might not be available with earlier versions of the standard.



Hence you can say that C++11 is not that identical language as C++98 / C++03 and also C++98 / C++03 varies from C++ before the language was standardized. If you did not read its slow-growing evolution, you may be surprised by the new language features the library uses.

Important Minor Syntax Cleanups

First, let's introduce the two new features of C++11 that are minor concepts but important for your day to day programming.

  • Spaces in Template Expressions: The necessity for putting a space between two closing template expressions got diminished. Something like this:
vector  <list <int>>; // Valid and fine in each C++ version

vector <list <int>>; // Valid and fine since C++11
  • nullptr and std :: nullptr_t: The new C++11 lets programmers use nullptr in place of 0 or NULL for specifying a pointer which refers to no value (i.e. it differs from including an undefined value). This additional characteristic especially helps to avoid mistakes which might occur when a null pointer gets interpreted as an integral value.

Let's take a small example based code snippet:

void g(int);

void g(void*);

g (0); // calls g(int)

g (NULL); // calls g(int) as and when NULL is 0 and ambiguous otherwise

g (nullptr); // calls g(void*)

Automatic Type Deduction using auto

Using C++11's new feature, you can declare within a program any variable or any object without specifying its exact type by using 'auto' keyword. Here are the lists of sample code snippet:

auto karl = 62; // karl has type int

double g();

auto s = g(); // s has type double

The type of a variable declared using the keyword auto is assumed from its initialized value / literal. Thus, for using this new feature an initialization is required:

auto gk; // ERROR: can't assume the type of variable gk

Additional qualifiers are also allowed. Let suppose:

static auto pi = 3.14;

Uniform Initialization and Initializer Lists

Earlier than C++11, programmers, particularly novices, could easily get confused with the question of how to initialize any variable or any object. Initialization can be performed using parentheses, braces, and/or assignment operators. So, C++11 launch the concept of uniform initialization wherein, for any initialization, you can write one common syntax. This syntax requires braces, so the following is possible now. Here is a list of examples:

int val [] { 2, 4, 6 };

std::vector vec { 1, 1, 2, 3, 5, 8, 13, 21, 34 };

std::vector  cities {

"Delhi", "New York", "California", "Cairo", "Albuquerque"

};

An initializer list forces the value initialization that means that even local variables of fundamental data types can be initialized with 0 (zero).

int k; // k is having undefined value

int g{}; // g is initialized with value 0


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram