Skip to the content of the web site.

Lesson 218: What is it with int n = 0;?

Previous lesson Next lesson


Up to this point, we have always initialized an identifier using braces; for example

	int n{0};
	int const PI{3.1415926535897932384626433832795};
	std::string str{"Hi there!"};
	int *p_head{nullptr};
	int primes[18]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61};
	Class_name inst_1{32};
	Class_name inst_3{};

In older C++ programs, as well as in C and Java programs, you may however have seen seen a declaration together with an assignment; for example

	int n = 0;
	int m(91);
	int const PI = 3.1415926535897932384626433832795;
	std::string str = "Hi there!";
	int *p_head = nullptr;
	int primes[18] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61};
	Class_name inst_1(32);
	Class_name inst_2 = 32;
	Class_name inst_3;

Both are valid C++, but the approach using braces is more consistent and the accepted approach in the C++ 2011 standard. The benefit is that all identifiers are initialized in the same way; that is, through the use of braces.

To explain the variation in ways of initializing variables, we will embark on a small history lesson:

For many decades, you could initialize variables using the assignment operator, and you could initialize an array using the assignment of a brace-delimited list of the appropriate size:

	int n = 0;
	int primes[18] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61};

With the introduction of C++, objects could be passed to the constructor using the notation of function calls; while if you assigned to an object, it would attempt to call the correct constructor or assignment operator:

	Class_name inst_1(32);
	Class_name inst_2 = 32;

Now, you had to be careful, for if an object you were creating did not take any arguments to the constructor, you could not use what should be the obvious notation:

	Class_name inst_1();

This is because the compiler may become confused: is inst_1 an instance of the class Class_name; or is this a function declaration, where inst_1 is being declared as a function that has no parameters and returns an instance of the class Class_name?

This ambiguity was recognized early on in C++, but the solution was not worked up until the C++ 2011 standard. The language authors saw that braces were used in only two situations: to delimit blocks of code, and to initialize arrays. It was determined, however, that braces were only used in two situations, and if they were used to indicate default values for all identifiers being declared, then this would be ideal. Thus, while there was ambiguity with respect to the previous notations, braces following a declared identifier must imply initialization.

If you are in a workplace, determine which approach is used by your colleagues and use that approach; do not attempt to enforce "good C++11 standards" on them; this will not be appreciated.


Previous lesson Next lesson