|
|
|
|
|
by DannyBee
1168 days ago
|
|
No, it doesn't depend on the values.
Yes, it does follow.
The compiler is not allowed to compile constexpr code that could produce UB at runtime. period. :) That is, conditional constepxr code that depends on values and could produce UB is not valid constexpr code, and a compiler is not supposed to compile it. This is very explicit in the standard. Think of it as a statically decidable set of code. Now, it wouldn't shock me if compilers don't achieve this right now, but the standard is clear that constexpr code may not contain operations that could produce undefined behavior at runtime. |
|
A constexpr function can very well take an input, and it can invoke UB based on the runtime value of that input.
What the standard prohibits is compile-time evaluation of an expression which invokes UB. So, if you actually call your function at compile-time with a constexpr value that ends up invoking UB in the function (say, an integer overflow), THEN the standard mandates that the compiler throw an error rather than compiling some random value in.
For example:
Edit: I should also add that you can very well invoke UB in a constexpr expression if it is standard library UB and not core language UB (e.g. if you try to pop() from an empty std::vector).