Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My mistake, or bug in intel compiler? sizeof a non-static member error

Tags:

c++

c++11

sizeof

It is my belief that this code:

#include <stdio.h>

struct foo {
    char array[1024];
};

int main() { 
    fprintf(stderr, "sizeof(foo::array): %zd\n", sizeof(foo::array));    
}

Is valid C++. g++ compiles it just fine with -ansi -pedantic. However, compiling with Intel's icc 12.1.3 I get:

error #288: a nonstatic member reference must be relative to a specific object

Is it my mistake or is icc doing the wrong thing re: the C++ spec?

like image 275
gct Avatar asked Nov 24 '15 14:11

gct


1 Answers

It is a compiler bug or maybe the compiler was issued before this feature was adopted in the Standard.

According to the C++ Standard (5.1 Primary expressions)

13 An id-expression that denotes a non-static data member or non-static member function of a class can only be used:

— if that id-expression denotes a non-static data member and it appears in an unevaluated operand.

[ Example:
struct S {
int m;
};
int i = sizeof(S::m); // OK
int j = sizeof(S::m + 42); // OK
—end example ]
like image 127
Vlad from Moscow Avatar answered Sep 23 '22 13:09

Vlad from Moscow