Is the compiler guaranteed to evaluate boolean constexpr
expressions that are "tautologies"
(e.g. always true
or false
respectively) in a constexpr
environment?
Minimal Example / Clarification
For example, in the following code snipplet (at the line marked with (1)
) I invoke a function in a constexpr
environment, which I intend to cause a compile time error whenever a non-constexpr
function is passed. At least the compiler I use (g++-10.0
) does this, even though it could also realize that the expression is always true
without evaluating it. The reason I ask this question, is because -- to the best of my knowledge -- in a non-constepxr context, an expression like i >= std::numeric_limits<int>::min()
is optimized away to true
for an int i
.
#include <limits>
constexpr int example_function() { return 1;}
constexpr bool compileTimeErrorDesired = example_function() || true; // (1)
Application Example
If the behavior in (1)
is guaranteed, it can for instance be used in a concept
, in order to execute different code, depending on whether a function provided as template argument can be evaluated at compile time. I implemented a very short (7 lines-of-code
) example which does exactly that here at compiler explorer.
Question
Is the line (1) guaranteed to cause a compile time error if called with a non-constexpr function?
edit Inserted clarification, simplify example due to feedback.