Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can expression using pointers causing unspecified (not undefined!) behaviour be used in constexpr context?

According to cppreference (emphasis mine):

A core constant expression is any expression that does not have any one of the following in any subexpression
(...)

  1. 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?

like image 607
W.F. Avatar asked Mar 03 '17 09:03

W.F.


1 Answers

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.

like image 74
Potatoswatter Avatar answered Nov 05 '22 08:11

Potatoswatter