![]() |
VOOZH | about |
C++17 enables writing simple, clearer, and more expressive code. Some of the features introduced in C++17 are:
Namespaces are a very convenient tool to organize and to structure the code base, putting together components like classes and functions that logically belong to the same group.
Let's consider a hypothetical code base of a video game engine. Here, defined a namespace for the whole game engine, so all the classes and the functions implemented in this Game Engine will be declared under this common namespace. To do more clear definitions you can define another namespace under the global namespace lets say Graphics which is a sub-namespace, now put all classes that perform graphics operations under that namespace and so on.
Before C++17 you have to use this verbose syntax for declaring classes in nested namespaces, but C++17 has introduced a new feature that makes it possible to open nested namespaces without this hectic syntax that require repeated namespace keyword and keeping track of opening and closing braces. In C++17 there a simple and concise syntax using double colons to introduce nested namespaces. The syntax is as follows:
This makes the code less error-prone as there is no need to pay attention to several levels of braces.
Before C++17:
Suppose a vector of strings and you want to replace a string "abc" with "$$$" if it is present in the vector. To do that you can invoke the standard find() function to search for an item in a vector as shown below:
Explanation:
When C++17:
For dealing with such cases C++17 gives a better option by declaring variables inside if statements as shown below:
Now the scope of the iterator βitβ is within the if statement itself, and the same iterator name can be used to replace other strings too.
The same thing can be done using a switch statement to using the syntax given below:
switch (initial-statement; variable) {
....
// Cases
}
Below is the program that replaces some defined strings in the given vector that runs only in C++17:
Output:
π ImageThis feature of C++ 17 is very useful when you write template code. The normal if statement condition is executed at run time, so C++17 introduced this new if constexpr statement. The main difference is that if constexpr is evaluated at compile time. Basically, constexpr function is evaluated at compile-time. So why is this important, its main importance goes with template code.
Before C++17:
Suppose, to compare if an integer variable with a value, then declare and initialize that integer at compile time only before using that variable as shown below:
When C++17:
Suppose you have some template that operates on some generic type T.
So one aspect of constexpr is that now the compiler knows if T is an integer or not, and the compiler considers only the substatement that satisfies the condition so only that block of code is compiled and the C++ compiler ignores the other substatements.
Below is the program for the same:
Output:
error: request for member 'length' in 'value', which is of non-class type 'const int'
Explanation:
Below is the correct code:
Output:
π ImageIt basically allows you to declare multiple variables that are initialized with values from pairs, generic tuples or from custom structures and these multiples variable declarations happens in single statements.
Before C++17:
Before C++17, std::tie was used to declare multiple variables that are initialized with values from custom structures.
When C++17:
Suppose you have a dictionary having names as keys and their favorite language as values and this is implemented using standard container map and you want to insert a new entry to it using insert method. This insert method returns an std::pair containing two pieces of information, the first item in the pair is an iterator and the second item is a boolean value.
There are two cases to consider here:
Now to write the code to inspect the boolean flag and insertion iterator, first write .first and .second to access elements in pair. C++ 17 can do better for this as:
Below is the program for the same:
Output:
π ImageC++11 gave the option of variadic templates to work with variable number of input arguments. Fold expressions are a new way to unpack variadic parameters with operators. The syntax is as follows:
(pack op ...)
(... op pack)
(pack op ... op init)
(init op ... op pack)
where pack represents an unexpanded parameter pack, op represents an operator and init represents a value.
Before C++17:
To make a function that takes variable number of arguments and returns the sum of arguments.
When C++17:
To implement a recursive function like sum etc through variadic templates, this becomes efficient with C++17 which is better than C++11 implementations. Below is the template class of the same:
Below is the program to illustrate the same:
Output:
π ImageIn C++ 17 initialize initialization of enums using braces is allowed. Below is the syntax for the same:
enum byte : unsigned char {};
byte b {0}; // OK
byte c {-1}; // ERROR
byte d = byte{1}; // OK
byte e = byte{256}; // ERROR
Some of the library features of C++17:
Output:
π Image