【发布时间】:2015-11-21 07:24:32
【问题描述】:
offsetof 可以与通过decltype 获得的类型一起使用吗?这些情况中的任何一种都有效 C++11 吗?
struct S {
int i;
int j { offsetof(decltype(*this), i) }; // case 1
S() : i(offsetof(decltype(*this), j)) {}; // case 2
} inst1;
int main() {
struct {
int i;
int j { offsetof(decltype(*this), i) }; // case 3
} inst2;
return 0;
}
在 Apple LLVM 版本 6.0 (clang-600.0.57)(基于 LLVM 3.5svn)下均无法编译,出现错误
error: offsetof requires struct, union, or class type,
'decltype(*this)' (aka '<anonymous struct at ../qxjs3uu/main.cpp:4:4> &') invalid
它似乎也使 MSVC 19.00.23106.0(x86) 崩溃并出现内部错误:
Compiled with /EHsc /nologo /W4 /c
main.cpp
main.cpp(3): error C2062: type 'S &' unexpected
[...]
main.cpp(4): fatal error C1903: unable to recover from previous error(s); stopping compilation
Internal Compiler Error in c:\tools_root\cl\bin\i386\cl.exe. You will be prompted to send an error report to Microsoft later.
我是否想到了测试用例编写者没有想到的东西?
【问题讨论】: