Initialization in C++ 11

The initialization with single-valued variables

The use of initializer with braces for a variable with a single value was not common practice, but the C ++ 11 standard extends this way.

So, for define a ‘iX’ variable with a single value, we can do it by following way:

int iX = {8} // set iX to 8

But same operation, also can be used  without  the ‘=’  sign:
For example:

int iX {9} // set iX to 9

The braces can be left empty, in which case the variable is initialized to 0:

int iY = {} // set iY to 0


int iY {} // set iY to 0

Form initialization braces were added to C++ 11 in order to make initializing ordinary variables more like initializing class variables. C++11 syntax allows the use of braces (with or without the = sign) with all types – it is a universal syntax for initializing.

C++11 extends the applicability of the brace-enclosed list (list-initialization) for arrays, too (the list can be used either with or without the = sign):

For example:

short arr[4] {1,2,3,4};

Also the list-initialization syntax can be used in new expressions:

int * pArr = new int [4] {1,2,3,4};

Yes, of course, new standard of language enables list-initialization for C-style strings and string objects. For example:


int main()
  char char_array1 [] = {"AAA"};
  char char_array2 [] {"BBB"};

  std::string str1    = {"CCC"};
  std::string str2 {"DDD"};

  std::cout << "char_array1  = "  << char_array1 <<"\n";
  std::cout << "char_array2 = "   << char_array2 <<"\n";
  std::cout << "str1  = "         << str1        <<"\n";
  std::cout << "str2 = "          << str2        <<"\n";

  return 0;

Output in gcc++ 4.8:

C++11 extends also  the applicability of the  list-initialization, so that it can be used with all built-in types and with user-defined types (for example, like class or struct ).The list we can use  with or without the ‘=’ sign:


But, if a class has a constructor argument std::initializer_list template,  then only that constructor can use the specific form of  list-initialization.

The initialization-list syntax  avoids a lot of incompatibilities by providing  protection against narrowing


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s