我相信这段代码:
#include <stdio.h>
struct foo {
    char array[1024];
};
int main() { 
    fprintf(stderr, "sizeof(foo::array): %zd\n", sizeof(foo::array));    
}
Run Code Online (Sandbox Code Playgroud)
是有效的C++.g ++用-ansi -pedantic编译得很好.但是,使用英特尔的icc 12.1.3进行编译,我得到:
error #288: a nonstatic member reference must be relative to a specific object
Run Code Online (Sandbox Code Playgroud)
这是我的错误还是icc做了错误的事情:C++规范?
这是编译器错误,或者编译器可能是在标准采用此功能之前发布的。
\n\n根据 C++ 标准(5.1 基本表达式)
\n\n\n\n\n13 只能使用表示类的非静态数据成员或非静态成员函数的 id 表达式:
\n\n\xe2\x80\x94 如果该 id 表达式表示非静态数据成员并且\n 出现在未计算的操作数中。
\n
[ Example:\nstruct S {\nint m;\n};\nint i = sizeof(S::m); // OK\nint j = sizeof(S::m + 42); // OK\n\xe2\x80\x94end example ]\nRun Code Online (Sandbox Code Playgroud)\n