1

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.

3
  • The sizeof(i) is really sizeof(this->i), but you are not inside a function, so there is no this. – Raymond Chen Oct 22 '15 at 0:17
  • @RaymondChen Is that part of the standard (I don't know)? OP mentions this works on GCC and CLang. If this is the case, are those then incorrectly not catching this? – Steve Oct 22 '15 at 1:25
  • "The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized type-id." This is not a parenthesized type-id, so all that's left is an expression. But i is not a valid expression at that point because there is no object i in scope. The closest thing is this->i but there is no this. I suspect that GCC added support for this as a nonstandard extension. – Raymond Chen Oct 22 '15 at 2:13
0

I had a simular issue and the problem was that MSVC 2013 does not support constant expressions thus both

static_assert(sizeof(i) > 1, "Wrong size");

and

char array[sizeof(i)];

require constant expressions which is not supported by the compiler.

This is valid in gcc though

0

Interestingly, sizeof (decltype(i)) works in VS2013.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.