Professional C__ - Marc Gregoire [150]
Alternative Function Syntax
C++ is still using the function syntax as it was designed for C. In the meantime, C++ has been extended with quite a lot of new functionality and this exposed a number of problems with the old function syntax. C++11 includes an alternative function syntax as follows. Note that the auto keyword in this context has the meaning of starting a function prototype using the new alternative function syntax.
auto func(int i) -> int
{
return i+2;
}
Code snippet from AlternativeFunctionSyntax\AlternativeFunctionSyntax.cpp
The return type of the function is no longer at the beginning of the declaration, but placed at the end of the line after the arrow, ->. The following code demonstrates that calling func() remains exactly the same and shows that the main() function can also use this alternative syntax:
auto main() -> int
{
cout << func(3) << endl;
return 0;
}
Code snippet from AlternativeFunctionSyntax\AlternativeFunctionSyntax.cpp
This new syntax is not of much use for ordinary functions, but is very useful in the context of specifying the return type of template functions. Templates are studied in detail in Chapters 19 and 20.
Null Pointer Constant
Before C++11, the constant 0 was used to define either the number 0 or a null pointer. This can cause some problems. Take the following example:
void func(char* str) {cout << "char* version" << endl;}
void func(int i) {cout << "int version" << endl;}
int main()
{
func(NULL);
return 0;
}
Code snippet from NullPointerConstant\NullPointerConstant.cpp
The main() function is calling the function func() with parameter NULL, which is supposed to be a null pointer constant. In other words, you are expecting the char* version of func() to be called with a null pointer as argument. However, since NULL is identical to the integer 0, the compiler will actually call the integer version of func(), as can be seen in the output of the program:
int version
You could call the char* version by explicitly using a cast:
func((char*)NULL);
Code snippet from NullPointerConstant\NullPointerConstant.cpp
This will print:
char* version
C++11 solves this problem more elegantly by introducing a real null pointer constant, nullptr, which can be used as follows:
func(nullptr);
Code snippet from NullPointerConstant\NullPointerConstant.cpp
This will generate the following output:
char* version
Angle Brackets
Before C++11, the double angle brackets >> could only mean one thing: the >> operator. Depending on the types involved, this >> operator can be a right shift operation or a stream extraction operator. This caused small problems with template code. For example, if you wanted to declare a vector for objects of type basic_string vector This was invalid because of the double angle brackets >> without a space in between. The lexical analyzer would interpret it as the >> operator. Instead, you had to make sure to include a space between the two brackets as follows: vector C++11 solves this problem, and the first version without the space is perfectly valid now. Initializer Lists C++11 adds the concept of initializer lists, defined in the #include using namespace std; int makeSum(initializer_list { int total = 0; for (auto iter = lst.begin(); iter != lst.end(); ++iter) total += (*iter); return total; } Code snippet from InitializerLists\InitializerLists.cpp The function makeSum() accepts an initializer list of integers as argument. The body of the function uses an iterator to loop