constexpr 指定子を指定して宣言される非コンストラクター関数は、constexpr コンストラクター関数です。 constexpr 関数は、定数式内で呼び出すことができる関数です。
コンストラクターではない非静的メンバー関数が constexpr 指定子を指定して宣言されている場合、メンバー関数は定数であり、constexpr 指定子には関数型に対するその他の効果はありません。 その関数がメンバーであるクラスは、リテラル型でなければなりません。
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
}
constexpr 関数への呼び出しは、あらゆる点で、同等の非 constexpr 関数への呼び出しと同じ結果を生みます。ただし、constexpr 関数への呼び出しは定数式内に指定できるという点が異なります。
constexpr 関数は暗黙的にインラインになります。
main 関数は、constexpr 指定子を指定して宣言することはできません。