Can offsetof
be used with a type obtained through decltype
? Is either of those cases valid 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 of it compiles under Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn), with the error
error: offsetof requires struct, union, or class type,
'decltype(*this)' (aka '<anonymous struct at ../qxjs3uu/main.cpp:4:4> &') invalid
It also seems to crash MSVC 19.00.23106.0(x86) with an internal error:
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.
Did I think of something that no test case writers thought of?
The result of dereferencing a pointer is an lvalue (and itself is an expression), thus decltype(*this)
gives you type 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
;
To use it as an argument to offsetof
, you'd need to remove a reference from a type obtained from the decltype()
specifier:
offsetof(std::remove_reference<decltype(*this)>::type, i)
DEMO
They can be used together. Take the example:
#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;
}
Which prints offset: 4
I think your problem stems from using decltype(*this), which I'm not sure will do what you're expecting according to 5.1.1 of the C++11 standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With