According to cppreference (emphasis mine):
A core constant expression is any expression that does not have any one of the following in any subexpression
(...)
- An expression whose evaluation leads to any form of core language undefined behavior (including signed integer overflow, division by zero, pointer arithmetic outside array bounds, etc). Whether standard library undefined behavior is detected is unspecified.
On the other hand there are several expressions on pointers with a result that isn't undefined but unspecified (cf. [expr.rel]/3) e.g.:
struct A {
int v;
};
struct B {
int v;
};
struct C: A, B {} c;
int main() {
constexpr bool result = &c.A::v < &c.B::v;
(void)result;
}
The code compiles without issues with gcc but not in clang which states what is doubtlessly true that:
comparison of addresses of subobjects of different base classes has unspecified
But (as I understand it) according to cppreference it should not stop compiler from compiling the code.
Which compiler is right here - gcc or clang? Am I over-interpreting cppreference?
In addition to the catch-all case regarding UB, towards the end of the list of forbidden expressions in [expr.const] is,
— a relational or equality operator where the result is unspecified
This also appears in cppreference list, currently numbered #19.
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