When I define a constexpr
function, should I also declare it as noexcept
? I imagine in the case where the arguments and usage satisfy the requirements for compile-time evaluation, the meaning of potential exceptions is moot. But it would apply as normal for cases when the function is evaluated at run time.
As a matter of practical concern, if the function is indeed simple, perhaps using built-in arithmetic or a cast, such that I expect the compiler can always inline the function and optimize across it, does it matter to the efficiency of the generated code if I leave off noexcept
?
No, in general it doesn't. A constexpr function can be invoked in a non-constepr context in which it is allowed to throw an exception (except of course if you manually specify it to be noexcept(true) ).
A constexpr function is one whose return value is computable at compile time when consuming code requires it. Consuming code requires the return value at compile time to initialize a constexpr variable, or to provide a non-type template argument.
Even though try blocks and inline assembly are allowed in constexpr functions, throwing exceptions or executing the assembly is still disallowed in a constant expression.
A constexpr constructor is implicitly inline.
No, you should not.
"Cannot fail" and "can be evaluated at compile time" are orthogonal issues. For example, you want to write an integer power function, but you want to take the power as signed (because you believe that unsigned numbers should only be used for very special cases). Now you say you want to throw an exception if the power is negative. In C++14 you can write it like this:
constexpr int ipow(int base, int power) {
if (power < 0) throw std::domain_error("negative power");
int result = 1;
for (int i = 0; i < power; ++i) result *= base;
return result;
}
This function clearly isn't noexcept, but for non-negative arguments, you obviously can evaluate it at compile time. If you try it for negative arguments in a context where you need the result at compile time, you get a compile error, which should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With