offsetof 可以与从 decltype 获得的结构类型一起使用吗?
Can offsetof be used with a struct type obtained from decltype?
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;
}
None在Apple LLVM version 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.
我是不是想到了测试用例编写者没有想到的东西?
它们可以一起使用。举个例子:
#include <iostream>
#include <string>
#include <stddef.h>
int main()
{
struct S {
int a;
int b;
};
S instance;
std::cout << "offset: " << offsetof(decltype(instance), b) << "\n";
return 0;
}
打印偏移量:4
我认为你的问题源于使用 decltype(*this),根据 C++11 标准的 5.1.1,我不确定它是否能满足你的期望。
取消引用指针的结果是一个左值(它本身是一个表达式),因此 decltype(*this)
给你类型 S&
:
§ 7.1.6.2 [dcl.type.simple]/p4:
The type denoted by decltype(e)
is defined as follows:
— [...]
— otherwise, if e
is an lvalue, decltype(e)
is T&
, where T
is the type of e
;
要将其用作 offsetof
的参数,您需要从从 decltype()
说明符获得的类型中删除引用:
offsetof(std::remove_reference<decltype(*this)>::type, i)
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;
}
None在Apple LLVM version 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.
我是不是想到了测试用例编写者没有想到的东西?
它们可以一起使用。举个例子:
#include <iostream>
#include <string>
#include <stddef.h>
int main()
{
struct S {
int a;
int b;
};
S instance;
std::cout << "offset: " << offsetof(decltype(instance), b) << "\n";
return 0;
}
打印偏移量:4
我认为你的问题源于使用 decltype(*this),根据 C++11 标准的 5.1.1,我不确定它是否能满足你的期望。
取消引用指针的结果是一个左值(它本身是一个表达式),因此 decltype(*this)
给你类型 S&
:
§ 7.1.6.2 [dcl.type.simple]/p4:
The type denoted by
decltype(e)
is defined as follows:— [...]
— otherwise, if
e
is an lvalue,decltype(e)
isT&
, whereT
is the type ofe
;
要将其用作 offsetof
的参数,您需要从从 decltype()
说明符获得的类型中删除引用:
offsetof(std::remove_reference<decltype(*this)>::type, i)