template<typename T> constexpr inline
T getClamped(const T& mValue, const T& mMin, const T& mMax)
{
assert(mMin < mMax); // remove this line to successfully compile
return mValue < mMin ? mMin : (mValue > mMax ? mMax : mValue);
}
error: body of constexpr function 'constexpr T getClamped(const T&, const T&, const T&) [with T = long unsigned int]' not a return-statement
Using g++ 4.8.1
. clang++ 3.4
doesn't complain.
Who is right here? Any way I can make g++
compile the code without using macros?
A constexpr function that is eligible to be evaluated at compile-time will only be evaluated at compile-time if the return value is used where a constant expression is required. Otherwise, compile-time evaluation is not guaranteed.
It means that compiler can't guarantee that your if-constexpr is always compile-time, hence the error about it. If you place all your arguments as template auto parameters of your function then it should compile well.
Quick A: constexpr guarantees compile-time evaluation is possible if operating on a compile-time value, and that compile-time evaluation will happen if a compile-time result is needed.
A constexpr function is a function that can be invoked within a constant expression. A constexpr function must satisfy the following conditions: It is not virtual. Its return type is a literal type.
constexpr functions can only depend on functionality which is a constant expression. Being a constexpr function does not mean that the function is executed at compile time. It says, that the function has the potential to run at compile time. A constexpr function can also run a run time.
It turns out that it’s remarkably easy to do. Static asserts are another handy feature introduced in C++11. They let you verify compile-time conditions, such as template parameters and data type sizes. Helpfully, you can call constexpr functions as part of their condition.
Static asserts are another handy feature introduced in C++11. They let you verify compile-time conditions, such as template parameters and data type sizes. Helpfully, you can call constexpr functions as part of their condition. Obviously, if the function cannot be evaluated at compile-time then attempting to do this will fail.
A constexpr function can have variables and modify them. A metafunction generates a new value. A metafunction uses recursion to simulate a loop. Instead of an end condition, a metafunction uses a full specialization of a template to end a loop.
GCC is right. However, there is a relatively simple workaround:
#include "assert.h"
inline void assert_helper( bool test ) {
assert(test);
}
inline constexpr bool constexpr_assert( bool test ) {
return test?true:(assert_helper(test),false);
}
template<typename T> constexpr
inline T getClamped(const T& mValue, const T& mMin, const T& mMax)
{
return constexpr_assert(mMin < mMax), (mValue < mMin ? mMin : (mValue > mMax ? mMax : mValue));
}
where we abuse the comma operator, twice.
The first time because we want to have an assert
that, when true
, can be called from a constexpr
function. The second, so we can chain two functions into a single constexpr
function.
As a side benefit, if the constexpr_assert
expression cannot be verified to be true
at compile time, then the getClamped
function is not constexpr
.
The assert_helper
exists because the contents of assert
are implementation defined when NDEBUG
is true, so we cannot embed it into an expression (it could be a statement, not an expression). It also guarantees that a failed constexpr_assert
fails to be constexpr
even if assert
is constexpr
(say, when NDEBUG
is false).
A downside to all of this is that your assert fires not at the line where the problem occurs, but 2 calls deeper.
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