[C++ Primer Chapter 6] constexpr function

 

constexpr function

Constexpr function: A  constexpr function refers to a function used for constant expressions. The return value type of the function and all formal parameter types must be literal values, and the function must have only one return statement.

1  [tect2.cpp]
 2 #include <iostream>
 3  using  namespace stdl;
 4  
5 constexpr int screen( int x)                // constexpr 
6  {
 7     return x; 
 8  }
 9  
10  int main()
 11  {   
 12      const  int x = 0 ;                      // const, is a constant expression because the value does not change 
13      constexpr int z = screen(x);         // constexpr
14     cout << z<<endl;
15     return 0;
16 }
1  [tect2.cpp]
 2  
3 #include <iostream>
 4  using  namespace std;
 5  
6 constexpr int screen( int x)
 7  {
 8     return x; 
 9  }
 10  
11  
12  int main()
 13  {   
 14      int x = 0 ;                 // not a constant expression because the value changes 
15      int z =   screen(x);
 16      cout << z<< endl;
 17      return  0;
18 }

The analysis  first explains the definition of constant expression: a constant expression is an expression whose value does not change and the result can be obtained at the compilation stage. 
(1) [test1.cpp] conforms to the normal usage of constexpr functions, that is, the return type of the function and all formal parameter types must be literal value types (the literal value type is the type that can get the result during the compilation process). At this point, if you change const int x = 0; to int x = 0;, an error will be reported, because the screen function is used in a context that requires constant expressions (variables of type constexpr must be initialized with constant expressions), compile The compiler will check whether the return value of the function is a constant expression during the compilation process, and will report an error if it is not. 
(2) [test2.cpp] indicates that the constexpr function can return a non-constant expression, and no error is reported during the compilation process, because the screen function is not used in a context that requires a constant expression, and the compiler will not be in the process of compiling. To check the return value of the function, there will be no error. (3) As a supplement, it should be noted that the constexpr function must have a return statement.
Conclusion : It can be thought like this: C++ does not require that constexpr functions must return constant expressions (take the screen constexpr function in the question as an example)

If a constant expression is not required in a context, such as: int z = screen(x); a constant expression may not be returned, and the compiler will not check whether the result of the function returns a constant expression.
If it is in a context where a constant expression is required, such as: constexpr int z = screen(x); then the constexpr function must be able to return a constant expression. At this time, the compiler will check whether the result returned by the function is a constant expression, and if not, it will report an error.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324662562&siteId=291194637