To add to what others have said, consider the following constexpr
function template:
template <typename T>
constexpr T add(T x, T y) { return x + y; }
This constexpr
function template is usable in a constant expression in some cases (e.g., where T
is int
) but not in others (e.g., where T
is a class type with an operator+
overload that is not declared constexpr
).
constexpr
does not mean that the function is always usable in a constant expression, it means that the function may be usable in a constant expression.
(There are similar examples involving nontemplate functions.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…