Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can offsetof be used with a struct type obtained from decltype?

Tags:

c++

c++11

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?

like image 790
Kuba hasn't forgotten Monica Avatar asked Feb 10 '23 05:02

Kuba hasn't forgotten Monica


2 Answers

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) is T&, where T is the type of e;

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

like image 112
Piotr Skotnicki Avatar answered Feb 11 '23 19:02

Piotr Skotnicki


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.

like image 44
patros Avatar answered Feb 11 '23 18:02

patros