A non-constructor function that is declared with a constexpr specifier is a constexpr function. A constexpr function is a function that can be invoked within a constant expression.
When a nonstatic member function that is not a constructor is declared with the constexpr specifier, that member function is constant, and the constexpr specifier has no other effect on the function type. The class of which that function is a member must be a literal type.
const int array_size1 (int x) {
return x+1;
}
// Error, constant expression required in array declaration
int array[array_size1(10)];
constexpr int array_size2 (int x) {
return x+1;
}
// OK, constexpr functions can be evaluated at compile time
// and used in contexts that require constant expressions.
int array[array_size2(10)];
struct S {
S() { }
constexpr S(int) { }
constexpr virtual int f() { // Error, f must not be virtual.
return 55;
}
};
struct NL {
~NL() { } // The user-provided destructor (even if it is trivial)
// makes the type a non-literal type.
};
constexpr NL f1() { // Error, return type of f1 must be a literal type.
return NL();
}
constexpr int f2(NL) { // Error, the parameter type NL is not a literal type.
return 55;
}
constexpr S f3() {
return S();
}
enum { val = f3() }; // Error, initialization of the return value in f3()
// uses a non-constexpr constructor.
constexpr void f4(int x) { // Error, return type should not be void.
return;
}
constexpr int f5(int x) { // Error, function body contains more than return statement.
if (x<0)
x = -x;
return x;
}
template <class C> constexpr NL f6(C c) { // OK, the constexpr specifier ignored
return NL();
}
void g() {
f6(55); // OK, not used in a constant expression
}
A call to a constexpr function produces the same result as a call to an equivalent non-constexpr function in all respects, except that a call to a constexpr function can appear in a constant expression.
A constexpr function is implicitly inline.
The main function cannot be declared with the constexpr specifier.