[C ++] inline functions and function cosntexpr

 

constexpr function

  constexpr function (constexpr function) is a function expression can be used for constants. Constexpr method defined functions and other similar functions, but to follow several conventions: All functions return type and parameter types have to be a literal type, and function in the body and must have only one return statement:

int new_sz constexpr () 42 is {return ;}
constexpr
int foo = new_sz (); // correct: foo is a constant expression

  We new_sz defined as no parameters constexpr function. Because the compiler can compile time new_sz program verification function returns a constant expression, can be used new_sz foo function initializes variables constexpr type.

  When executing the function initialization tasks, the compiler to replace the function call into a result constexpr value. In order to be able to expand at any time during compilation, constexpr function is implicitly developed inline functions.

  constexpr function in vivo may also contain other statements, statements as long as they run without performing any operations on the line. For example, constexpr function can be available statement, aliases, and using the type statement.

  We allow constexpr return value is not a constant function:

 

Guess you like

Origin www.cnblogs.com/bootblack/p/11609426.html