Book-en-us
- Preface
- Chapter 01: Towards Modern C++
- Chapter 02: Language Usability Enhancements
- Chapter 03: Language Runtime Enhancements
- Chapter 04 Containers
- Chapter 05 Smart Pointers and Memory Management
- Chapter 06 Regular Expression
- Chapter 07 Parallelism and Concurrency
- Chapter 08 File System
- Chapter 09 Minor Features
- Chapter 10 Outlook: Introduction of C++20
- Appendix 1: Further Study Materials
- Appendix 2: Modern C++ Best Practices
Chapter 02: Language Usability Enhancements
When we declare, define a variable or constant, and control the flow of code, object-oriented functions, template programming, etc., before the runtime, it may happen when writing code or compiler compiling code. To this end, we usually talk about language usability, which refers to the language behavior that occurred before the runtime.
2.1 Constants
nullptr
The purpose of nullptr
appears to replace NULL
. There are null pointer constants in the C and C++ languages,
which can be implicitly converted to null pointer value of any pointer type,
or null member pointer value of any pointer-to-member type in C++.
NULL
is provided by the standard library implementation and defined as an implementation-defined null pointer constant.
In C, some standard libraries defines NULL
as ((void*)0)
and some define it as 0
.
C++ does not allow to implicitly convert void *
to other types, and thus ((void*)0)
is not a valid implementation
of NULL
. If the standard library tries to define NULL
as ((void*)0)
, then compilation error would occur in the following code:
char *ch = NULL; |
C++ without the void *
implicit conversion has to define NULL
as 0
.
This still creates a new problem. Defining NULL
to 0
will cause the overloading feature in C++
to be confusing.
Consider the following two foo
functions:
void foo(char*); |
Then the foo(NULL);
statement will call foo(int)
, which will cause the code to be counterintuitive.
To solve this problem, C++11 introduced the nullptr
keyword, which is specifically used to distinguish null pointers, 0
. The type of nullptr
is nullptr_t
, which can be implicitly converted to any pointer or member pointer type, and can be compared equally or unequally with them.
You can try to compile the following code using clang++:
#include <iostream> |
The outputs are:
foo(int) is called |
From the output we can see that NULL
is different from 0
and nullptr
.
So, develop the habit of using nullptr
directly.
In addition, in the above code, we used decltype
and
std::is_same
which are modern C++ syntax.
In simple terms, decltype
is used for type derivation,
and std::is_same
is used to compare the equality of the two types.
We will discuss them in detail later in the decltype section.
constexpr
C++ itself already has the concept of constant expressions, such as 1+2, 3*4. Such expressions always produce the same result without any side effects. If the compiler can directly optimize and embed these expressions into the program at compile-time, it will increase the performance of the program. A very obvious example is in the definition phase of an array:
#include <iostream> |
In the above example, char arr_4[len_2]
may be confusing because len_2
has been defined as a constant.
Why is char arr_4[len_2]
still illegal?
This is because the length of the array in the C++ standard must be a constant expression,
and for len_2
, this is a const
constant, not a constant expression,
so even if this behavior is supported by most compilers, but it is an illegal behavior,
we need to use the constexpr
feature introduced in C++11, which will be introduced next,
to solve this problem; for arr_5
, before C++98 The compiler cannot know that len_foo()
actually returns a constant at runtime, which causes illegal production.
Note that most compilers now have their compiler optimizations. Many illegal behaviors become legal under the compiler's optimization. If you need to reproduce the error, you need to use the old version of the compiler.
C++11 provides constexpr
to let the user explicitly declare that the function or
object constructor will become a constant expression at compile time.
This keyword explicitly tells the compiler that it should verify that len_foo
should be a compile-time constant expression.
In addition, the function of constexpr
can use recursion:
constexpr int fibonacci(const int n) { |
Starting with C++14, the constexpr function can use simple statements such as local variables, loops, and branches internally. For example, the following code cannot be compiled under the C++11 standard:
constexpr int fibonacci(const int n) { |
To do this, we can write a simplified version like this to make the function available from C++11:
constexpr int fibonacci(const int n) { |
2.2 Variables and initialization
if-switch
In traditional C++, the declaration of a variable can declare a temporary variable int
even though it can be located anywhere, even within a for
statement,
but there is always no way to declare a temporary variable in the if
and switch
statements.
E.g:
#include <iostream> |
In the above code, we can see that the itr
variable is defined in the scope of
the entire main()
, which causes us to rename the other when a variable need to traverse
the entire std::vector
again. C++17 eliminates this limitation so that
we can do this in if(or switch):
if (const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3); |
Is it similar to the Go?
Initializer list
Initialization is a very important language feature,
the most common one is when the object is initialized.
In traditional C++, different objects have different initialization methods,
such as ordinary arrays, PODs (Plain Old Data,
i.e. classes without constructs, destructors, and virtual functions)
Or struct type can be initialized with {}
,
which is what we call the initialization list.
For the initialization of the class object,
you need to use the copy construct,
or you need to use ()
.
These different methods are specific to each other and cannot be generic.
E.g:
#include <iostream> |
To solve this problem,
C++11 first binds the concept of the initialization list to the type
and calls it std::initializer_list
,
allowing the constructor or other function to use the initialization list
like a parameter, which is the initialization of class objects provides
a unified bridge between normal arrays and POD initialization methods,
such as:
#include <initializer_list> |
This constructor is called the initialize list constructor, and the type with this constructor will be specially taken care of during initialization.
In addition to the object construction, the initialization list can also be used as a formal parameter of a normal function, for example:
public: |
Second, C++11 also provides a uniform syntax for initializing arbitrary objects, such as:
Foo foo2 {3, 4}; |
Structured binding
Structured bindings provide functionality similar to the multiple return values
provided in other languages. In the chapter on containers,
we will learn that C++11 has added a std::tuple
container for
constructing a tuple that encloses multiple return values. But the flaw
is that C++11/14 does not provide a simple way to get and define
the elements in the tuple from the tuple,
although we can unpack the tuple using std::tie
But we still have to be very clear about how many objects this tuple contains,
what type of each object is, very troublesome.
C++17 completes this setting, and the structured bindings let us write code like this:
#include <iostream> |
The auto
type derivation is described in the
auto type inference section.
2.3 Type inference
In traditional C and C++, the types of parameters must be clearly defined, which does not help us to quickly encode, especially when we are faced with a large number of complex template types, we must indicate the type of variables to proceed. Subsequent coding, which not only slows down our development efficiency but also makes the code stinking and long.
C++11 introduces the two keywords auto
and decltype
to implement type derivation, letting the compiler worry about the type of the variable. This makes C++ the same as other modern programming languages, in a way that provides the habit of not having to worry about variable types.
auto
auto
has been in C++ for a long time, but it always exists as an indicator of a storage type, coexisting with register
. In traditional C++, if a variable is not declared as a register
variable, it is automatically treated as an auto
variable. And with register
being deprecated (used as a reserved keyword in C++17 and later used, it doesn't currently make sense), the semantic change to auto
is very natural.
One of the most common and notable examples of type derivation using auto
is the iterator. You should see the lengthy iterative writing in traditional C++ in the previous section:
// before C++11 |
When we have auto
:
#include <initializer_list> |
Some other common usages:
auto i = 5; // i as int |
Since C++ 14, auto
can even be used as function arguments in generic lambda expressions,
and such functionality is generalized to normal functions in C++ 20.
Consider the following example:
auto add14 = [](auto x, auto y) -> int { |
Note:
auto
cannot be used to derive array types yet:
auto auto_arr2[10] = {arr}; // illegal, can't infer array type
2.6.auto.cpp:30:19: error: 'auto_arr2' declared as array of 'auto'
auto auto_arr2[10] = {arr};
decltype
The decltype
keyword is used to solve the defect that the auto keyword
can only type the variable. Its usage is very similar to typeof
:
decltype(expression) |
Sometimes we may need to calculate the type of an expression, for example:
auto x = 1; |
You have seen in the previous example that
decltype
is used to infer the usage of the type.
The following example is to determine
if the above variables x, y, z
are of the same type:
if (std::is_same<decltype(x), int>::value) |
Among them, std::is_same<T, U>
is used to determine whether
the two types T
and U
are equal. The output is:
type x == int |
tail type inference
You may think that whether auto
can be used to deduce the return type of a function. Still consider an example of an add function, which we have to write in traditional C++:
template<typename R, typename T, typename U> |
Note: There is no difference between typename and class in the template parameter list. Before the keyword typename appears, class is used to define the template parameters. However, when defining a variable with nested dependency type in the template, you need to use typename to eliminate ambiguity.
Such code is very ugly because the programmer must explicitly
indicate the return type when using this template function.
But in fact, we don't know what kind of operation
the add()
function will do, and what kind of return type to get.
This problem was solved in C++11. Although you may immediately
react to using decltype
to derive the type of x+y
,
write something like this:
decltype(x+y) add(T x, U y) |
But in fact, this way of writing can not be compiled.
This is because x
and y
have not been defined
when the compiler reads decltype(x+y).
To solve this problem, C++11 also introduces a trailing return type,
which uses the auto keyword to post the return type:
template<typename T, typename U> |
The good news is that from C++14 it is possible to directly derive the return value of a normal function, so the following way becomes legal:
template<typename T, typename U> |
You can check if the type derivation is correct:
// after c++11 |
decltype(auto)
decltype(auto)
is a slightly more complicated use of C++14.
To understand it you need to know the concept of parameter forwarding in C++, which we will cover in detail in the Language Runtime Enhancements chapter, and you can come back to the contents of this section later.
In simple terms, decltype(auto)
is mainly used to derive
the return type of a forwarding function or package,
which does not require us to explicitly specify
the parameter expression of decltype
.
Consider the following example, when we need to wrap the following
two functions:
std::string lookup1(); |
In C++11:
std::string look_up_a_string_1() { |
With decltype(auto)
, we can let the compiler do this annoying parameter forwarding:
decltype(auto) look_up_a_string_1() { |
2.4 Control flow
if constexpr
As we saw at the beginning of this chapter, we know that C++11 introduces the constexpr
keyword, which compiles expressions or functions into constant results. A natural idea is that if we introduce this feature into the conditional judgment, let the code complete the branch judgment at compile-time, can it make the program more efficient? C++17 introduces the constexpr
keyword into the if
statement, allowing you to declare the condition of a constant expression in your code. Consider the following code:
#include <iostream> |
At compile time, the actual code will behave as follows:
int print_type_info(const int& t) { |
Range-based for loop
Finally, C++11 introduces a range-based iterative method, and we can write loops that are as concise as Python, and we can further simplify the previous example:
#include <iostream> |
2.5 Templates
C++ templates have always been a special art of the language, and templates can even be used independently as a new language. The philosophy of the template is to throw all the problems that can be processed at compile time into the compile time, and only deal with those core dynamic services at runtime, to greatly optimize the performance of the runtime. Therefore, templates are also regarded by many as one of the black magic of C++.
Extern templates
In traditional C++, templates are instantiated by the compiler only when they are used. In other words, as long as a fully defined template is encountered in the code compiled in each compilation unit (file), it will be instantiated. This results in an increase in compile time due to repeated instantiations. Also, we have no way to tell the compiler not to trigger the instantiation of the template.
To this end, C++11 introduces an external template that extends the syntax of the original mandatory compiler to instantiate a template at a specific location, allowing us to explicitly tell the compiler when to instantiate the template:
template class std::vector<bool>; // force instantiation |
The ">"
In the traditional C++ compiler, >>
is always treated as a right shift operator. But actually we can easily write the code for the nested template:
std::vector<std::vector<int>> matrix; |
This is not compiled under the traditional C++ compiler, and C++11 starts with continuous right angle brackets that become legal and can be compiled successfully. Even the following writing can be compiled by:
template<bool T> |
Type alias templates
Before you understand the type alias template, you need to understand the difference between "template" and "type". Carefully understand this sentence: Templates are used to generate types. In traditional C++, typedef
can define a new name for the type, but there is no way to define a new name for the template. Because the template is not a type. E.g:
template<typename T, typename U> |
C++11 uses using
to introduce the following form of writing, and at the same time supports the same effect as the traditional typedef
:
Usually, we use
typedef
to define the alias syntax:typedef original name new name;
, but the definition syntax for aliases such as function pointers is different, which usually causes a certain degree of difficulty for direct reading.
typedef int (*process)(void *); |
Variadic templates
The template has always been one of C++'s unique Black Magic. In traditional C++, both a class template and a function template could only accept a fixed set of template parameters as specified; C++11 added a new representation, allowing any number, template parameters of any category, and there is no need to fix the number of parameters when defining.
template<typename... Ts> class Magic; |
The template class Magic object can accept an unrestricted number of typename as a formal parameter of the template, such as the following definition:
class Magic<int, |
Since it is arbitrary, a template parameter with a number of 0 is also possible: class Magic<> nothing;
.
If you do not want to generate 0 template parameters, you can manually define at least one template parameter:
template<typename Require, typename... Args> class Magic; |
The variable length parameter template can also be directly adjusted to the template function.
The printf
function in the traditional C, although it can also reach the call of an indefinite number of formal parameters, is not class safe. In addition to the variable-length parameter functions that define class safety, C++11 can also make printf-like functions naturally handle objects that are not self-contained. In addition to the use of ...
in the template parameters to indicate the indefinite length of the template parameters, the function parameters also use the same representation to represent the indefinite length parameters, which provides a convenient means for us to simply write variable length parameter functions, such as:
template<typename... Args> void printf(const std::string &str, Args... args); |
Then we define variable length template parameters, how to unpack the parameters?
First, we can use sizeof...
to calculate the number of arguments:
#include <iostream> |
We can pass any number of arguments to the magic
function:
magic(); // 0 |
Second, the parameters are unpacked. So far there is no simple way to process the parameter package, but there are two classic processing methods:
1. Recursive template function
Recursion is a very easy way to think of and the most classic approach. This method continually recursively passes template parameters to the function, thereby achieving the purpose of recursively traversing all template parameters:
#include <iostream> |
2. Variable parameter template expansion
You should feel that this is very cumbersome. Added support for variable parameter template expansion in C++17, so you can write printf
in a function:
template<typename T0, typename... T> |
In fact, sometimes we use variable parameter templates, but we don't necessarily need to traverse the parameters one by one. We can use the features of
std::bind
and perfect forwarding to achieve the binding of functions and parameters, thus achieving success. The purpose of the call.
3. Initialize list expansion
Recursive template functions are standard practice, but the obvious drawback is that you must define a function that terminates recursion.
Here is a description of the black magic that is expanded using the initialization list:
template<typename T, typename... Ts> |
In this code, the initialization list provided in C++11 and the properties of the Lambda expression (mentioned in the next section) are additionally used.
By initializing the list, (lambda expression, value)...
will be expanded. Due to the appearance of the comma expression, the previous lambda expression is executed first, and the output of the parameter is completed.
To avoid compiler warnings, we can explicitly convert std::initializer_list
to void
.
Fold expression
In C++ 17, this feature of the variable length parameter is further brought to the expression, consider the following example:
#include <iostream> |
Non-type template parameter deduction
What we mainly mentioned above is a form of template parameters: type template parameters.
template <typename T, typename U> |
The parameters of the template T
and U
are specific types.
But there is also a common form of template parameter that allows different literals
to be template parameters, i.e. non-type template parameters:
template <typename T, int BufSize> |
In this form of template parameters, we can pass 100
as a parameter to the template.
After C++11 introduced the feature of type derivation, we will naturally ask, since the template parameters here.
Passing with a specific literal, can the compiler assist us in type derivation,
By using the placeholder auto
, there is no longer a need to explicitly specify the type?
Fortunately, C++17 introduces this feature, and we can indeed use the auto
keyword to let the compiler assist in the completion of specific types of derivation.
E.g:
template <auto value> void foo() { |
2.6 Object-oriented
Delegate constructor
C++11 introduces the concept of a delegate construct, which allows a constructor to call another constructor in a constructor in the same class, thus simplifying the code:
#include <iostream> |
Inheritance constructor
In traditional C++, constructors need to pass arguments one by one if they need inheritance, which leads to inefficiency. C++11 introduces the concept of inheritance constructors using the keyword using:
#include <iostream> |
Explicit virtual function overwrite
In traditional C++, it is often prone to accidentally overloading virtual functions. E.g:
struct Base { |
SubClass::foo
may not be a programmer trying to overload a virtual function, just adding a function with the same name. Another possible scenario is that when the virtual function of the base class is deleted, the subclass owns the old function and no longer overloads the virtual function and turns it into a normal class method, which has catastrophic consequences.
C++11 introduces the two keywords override
and final
to prevent this from happening.
override
When overriding a virtual function, introducing the override
keyword will explicitly tell the compiler to overload, and the compiler will check if the base function has such a virtual function with consistent function signature, otherwise it will not compile:
struct Base { |
final
final
is to prevent the class from being continued to inherit and to terminate
the virtual function to continue to be overloaded.
struct Base { |
Explicit delete default function
In traditional C++, if the programmer does not provide it, the compiler will default to generating default constructors, copy constructs, assignment operators, and destructors for the object. Besides, C++ also defines operators such as new
delete
for all classes. This part of the function can be overridden when the programmer needs it.
This raises some requirements: the ability to accurately control the generation of default functions cannot be controlled. For example, when copying a class is prohibited, the copy constructor and the assignment operator must be declared as private
. Trying to use these undefined functions will result in compilation or link errors, which is a very unconventional way.
Also, the default constructor generated by the compiler cannot exist at the same time as the user-defined constructor. If the user defines any constructor, the compiler will no longer generate the default constructor, but sometimes we want to have both constructors at the same time, which is awkward.
C++11 provides a solution to the above requirements, allowing explicit declarations to take or reject functions that come with the compiler. E.g:
class Magic { |
Strongly typed enumerations
In traditional C++, enumerated types are not type-safe, and enumerated types are treated as integers, which allows two completely different enumerated types to be directly compared (although the compiler gives the check, but not all), ** Even the enumeration value names of different enum types in the same namespace cannot be the same**, which is usually not what we want to see.
C++11 introduces an enumeration class and declares it using the syntax of enum class
:
enum class new_enum : unsigned int { |
The enumeration thus defined implements type safety. First, it cannot be implicitly converted to an integer, nor can it be compared to integer numbers, and it is even less likely to compare enumerated values of different enumerated types. But if the values specified are the same between the same enumerated values, then you can compare:
if (new_enum::value3 == new_enum::value4) { // true |
In this syntax, the enumeration type is followed by a colon and a type keyword to specify the type of the enumeration value in the enumeration, which allows us to assign a value to the enumeration (int is used by default when not specified).
And we want to get the value of the enumeration value, we will have to explicitly type conversion, but we can overload the <<
operator to output, you can collect the following code snippet:
#include <iostream> |
At this point, the following code will be able to be compiled:
std::cout << new_enum::value3 << std::endl |
Conclusion
This section introduces the enhancements to language usability in modern C++, which I believe are the most important features that almost everyone needs to know and use:
- Auto type derivation
- Scope for iteration
- Initialization list
- Variable parameter template
Exercises
Using structured binding, implement the following functions with just one line of function code:
#include <string>
#include <map>
#include <iostream>
template <typename Key, typename Value, typename F>
void update(std::map<Key, Value>& m, F foo) {
// TODO:
}
int main() {
std::map<std::string, long long int> m {
{"a", 1},
{"b", 2},
{"c", 3}
};
update(m, [](std::string key) {
return std::hash<std::string>{}(key);
});
for (auto&& [key, value] : m)
std::cout << key << ":" << value << std::endl;
}Try to implement a function for calculating the mean with Fold Expression, allowing any arguments to be passed in.
Refer to the answer see this.
Changkun Ou © 2016-2024. The book is licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0, code is open sourced under the MIT License.
If you like the book, you could donate the author.