Is constexpr Possible in C 11?
Question:
Can C 11 determine if an expression is a compile-time constant expression (constexpr)?
Answer:
Yes, it is possible to produce a compile-time boolean value based on this determination using the following technique:
template<typename T> constexpr typename remove_reference<T>::type makeprval(T &&& t) { return t; } #define isprvalconstexpr(e) noexcept(makeprval(e))
This macro checks whether the expression e is a prvalue constant expression by utilizing the noexcept operator. If noexcept(makeprval(e)) evaluates to true, the expression e is a prvalue constant expression.
Explanation:
Prvalue constant expressions have certain restrictions:
The makeprval function is not declared as noexcept, so if e is a prvalue constant expression, the call makeprval(e) will be a constant expression. Thus, the noexcept operator will return true.
Limitations:
The isprvalconstexpr macro has some limitations:
The above is the detailed content of Can C 11 Determine if an Expression is a Compile-Time Constant?. For more information, please follow other related articles on the PHP Chinese website!