vendredi 3 mars 2017

Can expression using pointers with 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
(...)
7) 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 which result 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?

Aucun commentaire:

Enregistrer un commentaire