C++11, C++14, and C++17. I guess, you see the pattern. Later in this year, we will get a new C++ standard. In march 2017, the C++17 specification reached the Draft International Standard stage. Before I dive into the details, I will give you an overview of C++17.

Let me at first look at the big picture.

The big picture

Concerning C++98 to C++14, I only mentioned the big points. But, there is a C++ standard missing in my graphic: C++03. This is intentional because C++03 is a very small C++ standard. More like a bug-fix release to C++98. If you know C++, you know, that the first ISO standard C++98 and the ISO standard C++11 are big standards. That will not hold for C++14 and in particular for C++03.

So the question is. Is C++17 a big C++ standard or a small one? From my perspective, the answer is quite easy. C++17 is something in between C++14 and C++11. So, C++17 is neither big nor small. Why? Here comes my short answer.

Overview

C++17 has a lot to offer. That will hold for the core language and the library. Let's first look at the core language.

Core language

Fold expressions

C++11 supports variadic templates. These are templates that can accept an arbitrary number of arguments. The arbitrary number is held by a parameter pack. Additionally, with C++17 that you can directly reduce a parameter pack with a binary operator:

If T is a pointer, the if branch in line 3 will be compiled. If not, the else branch in line 5. Two points are important to mention. The function get_value has two different return types and both branches of the if statement have to be valid.

Consequently, what is possible with for statements is with C++17 possible with if and switch statements.

Initializers in if and switch statements

You can directly initialize your variable inside the if and switch statement.

auto [iter, succeeded] in line 3 automatically creates the two variables iter and succeeded. They will be destroyed at line 9.

One of these features that make programming less cumbersome. The same holds for template deduction of constructors.

Template deduction of constructors

A function template can deduce its type parameters from its function arguments. But that was not possible for a special function template: the constructor of a class template. With C++17, this statement is simply wrong. A constructor can deduce its type parameters from is constructor arguments.

The subtle difference is that the compiler can still copy the value myValue according to C++17 (line 3). But no copy will take place in line 5.

If a feature is not necessary anymore or its application is even dangerous, you should remove it. This will happen in C++17 with std::auto_ptr and trigraphs.

auto_ptr and trigraphs removed

auto_ptr

std::auto_ptr is the first smart pointer in C++.Its job is it to take care of one resource. But it had a big issue. If you copy a std::auto_ptr, a move operation will take place under the hood. That is the reason, we get std::unique_ptr with C++11 as the replacement. You can not copy a std::unique_ptr.

Trigraphs

Trigraphs are a sequence of three characters in the source code that are treated as if they are a single character. They will be necessary if your keyboard doesn't support the single characters.

If you want to write obfuscated code C++17 may be not your language anymore.

1
2
3
4
5
6
7

// trigraphs.cppint main()??<
??(??)??<??>();
??>

I guess, you know, what the program is doing? If not, you have to translate the trigraphs to their single character representation.

If you apply the table, you will solve the riddle. The program represents a lambda function that will be executed just-in-place.

1
2
3
4
5
6
7

// trigraphsLambda.cppint main(){
[]{}();
}

What's next?

That is easy. In the next post, I write about the library feature, we get with C++17. These are the string_view, the parallel STL and the filesystem library. Additionally, we will get the new data types std::any, std::optional, and std::variant.

Go to Leanpub/cpplibrary"What every professional C++ programmer should know about the C++ standard library".Get your e-book. Support my blog.

Comments

Excellent post. I used to bbe checking continuously this weblog and I'm impressed! Extremely useful information particularly the remaining section :) I take care of such info a lot. I was looking for this particular information foor a very lengthy time.Thank you aand best of luck.