2

I thought that the standard provided me a function to find the 1st element of a struct. I can't seem to find that though.

I'm scared of alignment: I don't think it's legal to reinterpret_cast into the 1st element of a struct is it?

So for example given struct { int first, int second } foo; how would I get the address of the 1st element in foo, if I didn't know that first is laid out as the 1st element? (Meaning that &foo.first is not a valid solution.)

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 1
    Looks like maybe you are looking for [`std::offsetof`](https://en.cppreference.com/w/cpp/types/offsetof). I'll try and find a dupe if that is the case – NathanOliver Dec 18 '18 at 16:04
  • 3
    Otherwise the address of the struct and the address of the first member is the same if it is a standard layout class. There is also a dupe about that. – NathanOliver Dec 18 '18 at 16:05
  • 1
    Maybe duplicate https://stackoverflow.com/questions/9254605/is-a-structs-address-the-same-as-its-first-members-address – Damien Dec 18 '18 at 16:10
  • What about using `__attribute__((packed))` ? So you know for sure there is no padding ? It might be wrong, just an idea. – kocica Dec 18 '18 at 16:11
  • 3
    I'm curious - when is it useful to know 'the address of the first element regardless of what that element is', as distinct from 'the address of the struct'? –  Dec 18 '18 at 16:26
  • @NathanOliver `offsetof` doesn't help cause I need to use the name of the element. And I need to do this without knowing the first element's name. – Jonathan Mee Dec 18 '18 at 16:39
  • Do you know the type of the first member? – NathanOliver Dec 18 '18 at 16:40
  • 2
    Why do you need this? Maybe this is an X-Y-question or someone gets an idea after you describe the use case – Thomas Sablik Dec 18 '18 at 16:40
  • @NathanOliver Hmmm... I do not. That's going to be a problem. Even if I find the pointer, I can only use this as a `void*` it's not like I'm getting the actual type out :( – Jonathan Mee Dec 18 '18 at 16:47
  • If you don't know it's type, or it's name, there really isn't anything you can do until reflection makes into into the standard. – NathanOliver Dec 18 '18 at 16:48
  • @NathanOliver I'm assuming that even if I did get the first address I wouldn't be able to find the type of that element? – Jonathan Mee Dec 18 '18 at 17:04
  • 1
    @JonathanMee You would not. `&name_of_struct` will give you the address to the first member, but t has the type of `decltype(name_of_struct)*`. There isn't a way to get the type of the the member from that. – NathanOliver Dec 18 '18 at 17:07

1 Answers1

1

I don't think it's legal to reinterpret_cast into the 1st element of a struct is it?

It is legal, but only with the precondition that the class is standard-layout. Standard draft:

[basic.compound]

Two objects a and b are pointer-interconvertible if:

...

  • one is a standard-layout class object and the other is the first non-static data member of that object, or, if the object has no non-static data members, any base class subobject of that object (10.3), or

...

If two objects are pointer-interconvertible, then they have the same address, and it is possible to obtain a pointer to one from a pointer to the other via a reinterpret_cast ...

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • So this is what I need but sadly I really need to be able to get the type as well :( That's out of scope for this question. I'll write another question at some point. – Jonathan Mee Dec 18 '18 at 17:07
  • @JonathanMee To get the type of first member in general, you need (static) reflection, which is not in C++ indeed. If you can constrain the type, then you could use a trait template with an alias to the type of first member. – eerorika Dec 18 '18 at 17:10