Can an expression using pointers causing unspecified behavior (not undefined!) Be used in the context of constexpr?

According to cppreference (my attention):

Kernel constant expression - any expression that does not have one of the following in any subexpression
(...)

  1. An expression whose evaluation leads to any form of the main language undefined behavior (including integer overflow of integers, division by zero, arithmetic of pointers outside the bounds of the array, etc.). Is the standard library undefined detected unspecified.

On the other hand, there are several expressions on pointers with a result that is not undefined but unspecified (see [expr.rel] / 3 ), for example:

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 problems with gcc , but not in clang , which says that it is undoubtedly true that:

comparing the addresses of subobjects of different base classes has an unspecified

But (as I understand it), according to cppreference, it should not stop the compiler from compiling the code.

Which compiler is gcc or clang here? Am I compacting cppreference?

+6
source share
1 answer

In addition to the whole UB case, at the end of the list of forbidden expressions in [expr.const] is

- relational or equality operator, where the result is unspecified

It also appears in the cppreference list, currently numbered # 19.

+4
source

Source: https://habr.com/ru/post/1015432/


All Articles