C++ is a powerful programming language, but its complexity has always put users off. Then C++ decided to make a change and has grown to this day to become one of the most popular languages in the programming community. C++ has some new features that are very useful, such as auto, lambda, constexpr, tuple, smart Pointers, and so on.

As a programming language, C++ has evolved a lot.

Of course, these changes did not happen overnight. There was a time when C++ lacked energy and people didn’t like the language very much.

However, the situation changed when the C++ Standards Committee decided to speed up the transition.

Since 2011, C++ has become an evolving, dynamic language, which many people expected.

Don’t make the mistake of thinking that the language has become easier, it hasn’t. It remains one of the most difficult programming languages widely used. But it is much more user-friendly than the previous version.

Today, we’re going to take a closer look at some of the new features that every developer should know about (they’ve been around for eight years since C++11). Note that this article skips over some advanced features that may be explored in more detail in a future article.

Auto concept

When C++11 first introduced auto, everything became much simpler.

The concept of auto is to let the C ++ compiler automatically infer the type of data at compile time, rather than requiring you to manually declare the type every time. If your data type is map<string,vector<pair<int,int>>> <string,vector<pair and so on, things become very convenient. </string,vector<pair

Look at the fifth line. It’s not hard to understand that you can’t declare something without an Initializer. As in line 5, the compiler cannot infer a data type.

Initially, the use of auto was very limited. In later versions, Auto became even more powerful!

In lines 7 and 8, I use curly braces to initialize. This feature is also new to C++11.

Remember, when using auto, you must make sure that your compiler can infer data types in some way.

Now the question is, what happens if I say auto A = {1, 2, 3}? Will there be compilation errors? Is that a vector?

In fact, C++11 introduced STD ::initializer_list<type>, and if declared as auto, initializer_list would be considered such a lightweight container.

Finally, as mentioned earlier, when you are working with complex data types, compiler inference about data types is useful.

Don’t forget to look at line 25! The expression auto [v1,v2] = itr. Second is a new feature of C++17. This is called structured binding. In previous versions, each variable had to be extracted separately, but structured binding makes the process much easier.

Also, if you want to get the data by reference, just add a notation like auto &[v1,v2] = itr.second, which is very concise.

Lambda expressions

C++11 introduced lambda expressions, which are very similar to anonymous functions in JavaScript. They are unnamed function objects and capture variables in different scopes based on some neat syntax. They can also be assigned to variables.

Lambda is useful when you want to implement something small in code quickly but don’t want to write a whole function for it. Another very common use is to use it as a comparison function.

There’s a lot of detail in the example above.

First, notice how much code you save when the list is initialized. Then there are the generic begin() and end(), which are also new to C++11. Then there are the lambda functions that act as data comparators. The argument to the lambda function is declared auto, which is new in C ++14. Before this, it is not possible to use auto as a function parameter.

Here we use square brackets [] to start a lambda expression. It defines the scope of a lambda function, which is how much permission it has on local variables and objects.

Here are some definitions in modern C ++ :

[] stands for empty. Therefore, you can’t use any local variables of external scope in a lambda expression. Only parameters can be used. [=] represents local objects (local variables and parameters) in scope that can be retrieved by value, meaning that you can only use them but cannot modify them. [&] represents a local object (local variable and parameter) in scope that can be retrieved by reference, that is, you can modify it as in the following example. [this] represents the value to get the this pointer. [A,& B] represents getting object A by value and object B by reference. So, if you want to convert data to another form in a lambda function, you can use a scope to use lambda, as shown in the following code.

In the example above, if you use the [factor] value to get the local variable in a lambda expression, you cannot change factor in line 5 because you have no right to do so. Don’t abuse your authority!

Finally, notice that var is a reference. This guarantees that any change in the lambda function will actually change the vector.

The initial state in an if or switch statement

When I learned about this feature in C ++17, I really liked it.

Obviously, you can now initialize variables and condition check them inside the if/switch block. This is very helpful in keeping your code compact and concise. The usual form is as follows:

Executes constexpr at compile time

Constexpr is cool!

Suppose you have some expression to evaluate, and its value does not change once initialized. You can precompute this value and use it as a macro. Or, as provided in C++11, you can use constexpr.

Programmers tend to minimize program running time. Therefore, if certain operations can be left to the compiler to do, the runtime burden can be reduced and thus time efficiency can be improved.

The above code is a common example of constexpr.

Since we declare the Fibonacci evaluation function to be constexpr, the compiler preevaluates the value of fib(20) at compile time. Const long long bigVal = fib(20) instead of const long long bigVal = 2432902008176640000;

Note that the argument passed is const. This is a very important point for a function declared as constexpr. The argument passed is also constexpr or const. Otherwise, the function executes as a normal function, that is, it is not preevaluated at compile time.

The variable can also be constexpr. In this case, as you can probably guess, these variables are also evaluated at compile time. Otherwise, a compilation error will occur.

Interestingly, later in C ++17, constExpr-if and constExpr-lambda were introduced.


Much like a pair, a tuple is a set of fixed-size values of various data types.

Sometimes it’s more convenient to use STD ::array than a tuple. Array is very similar to a normal C type Array, but has some features from the C++ standard library. This data structure is new in C++11.

Class template parameter inference

It’s a little long. Starting with C ++17, parameter inference also applies to the standard class template. Previously, this feature only supported functional templates.

As a result,

Type inference is done implicitly. This becomes much more convenient for tuples.

If you are not familiar with C++ templates, the above features may not be easy to understand.

Smart Pointers

Pointers may not work well.

Because C++ gives programmers a lot of freedom, sometimes that freedom can get in the way. In most cases, it’s the Pointers that do the opposite.

Fortunately, C++11 introduced smart Pointers, which are more convenient than the original Pointers of the past, can help developers avoid memory leaks by properly releasing Pointers, and also provide additional security.

I originally wanted to explore smart Pointers in detail in this article, but obviously there are so many important details that it’s worth a separate article, so I think an article will be coming soon.

The original address: https://medium.freecodecamp.o…