jeudi 23 juin 2016

Pointer to data member of incomplete type

Is the following code well-defined in C++? (*)

I'm having a hard time figuring out where to even look in the standard, and searching the web hasn't turned up anything concrete.

struct S;
struct T {
    constexpr T() = default;
    bool S::* a = nullptr;
    int b       = 42;
};
const T t{};
// Test. Compiled using: cl /W4 /WX /FAs filename.cpp
#include <stdlib.h>
int main() {
    if (t.b != 42) abort();
}

The reason I'm asking is because it works (or seems to) with newer versions of GCC and Clang (x86/x86_64), but fails(**) with Visual Studio 2015 Update 2 and Update 3 RC.

Before reporting a bug I'd like to be sure I'm not relying on undefined behavior or just not searching for the right terms.

I've also tried using /vmg and /vmb as mentioned in this post.

(*): I mostly care about C++14 and later, but I don't see any reason the answer shouldn't apply to C++11.
(**): If the code is well-defined it looks like a codegen bug where it isn't allocation room for the pointer. Changing struct S to struct S{} seems to make the code "work".

Aucun commentaire:

Enregistrer un commentaire