Here's my test case, simplified to a minimal test case:
#include <iostream>
struct Foo {
int i;
static_assert(sizeof(i) > 1, "Wrong size");
};
static_assert(sizeof(Foo::i) > 1, "Wrong size");
int main () {
Foo f{42};
static_assert(sizeof(f.i) > 1, "Wrong size");
std::cout << f.i << "\n";
}
This works fine on any version of GCC or Clang recent enough to support static_assert. But on MSVC 2015, the first static_assert gives me a compile error:
static-assert.cpp(4): error C2327: 'Foo::i': is not a type name, static, or enumerator
static-assert.cpp(4): error C2065: 'i': undeclared identifier
static-assert.cpp(4): error C2338: Wrong size
The other two asserts work as expected, if I remove the first one. I also tried it on VS2013 with the same results. The documentation for C2327 talks about nested class member access, which doesn't seem relevant in any way I can see. What's going on here? Which compiler is right?
(Edited to add a third assert to make the problem clearer.)
Further edit: It doesn't actually seem to have anything to do with static_assert, because this fails with the same error:
struct Foo {
int i;
char array[sizeof(i)];
};
Again, this works fine in other compilers.
sizeof(i)is reallysizeof(this->i), but you are not inside a function, so there is nothis. – Raymond Chen Oct 22 '15 at 0:17iis not a valid expression at that point because there is no objectiin scope. The closest thing isthis->ibut there is nothis. I suspect that GCC added support for this as a nonstandard extension. – Raymond Chen Oct 22 '15 at 2:13