Converting from C style struct offset to C++ style member pointer

M

Marcel Müller

When I get a struct offset from a C style API as int. I.e.:

struct S
{ int I1;
double D1;
int I2;
};

int offset = offsetof(S, I2);

Now in C++ code I would like to convert this offset to a type safe C++
member pointer of type int S::*. Older gcc versions simply accepted a
cast, newer version reject the cast.


Marcel
 
A

Asger Joergensen

Hi Marcel
%
Marcel said:
When I get a struct offset from a C style API as int. I.e.:

struct S
{ int I1;
double D1;
int I2;
};

int offset = offsetof(S, I2);

Now in C++ code I would like to convert this offset to a type safe C++ member
pointer of type int S::*. Older gcc versions simply accepted a cast, newer
version reject the cast.

I am curious, where do you use this offsetof I mean in what situations are
you using it ?
I've been programming for 15+ years and it is the first time I see this
offsetof define, usually I just cast to the struct name and then address
it's members by their name, leaving the offset part to the compiler.

Would you be so kind and explain the need and give a couple of small examples ?

Thanks in advance
Best regards
Asger-P
http://Asger-P.dk/software
Standards were invented, so we all can have our own.
 
F

Florian Weimer

* Marcel Müller:
Now in C++ code I would like to convert this offset to a type safe
C++ member pointer of type int S::*. Older gcc versions simply
accepted a cast, newer version reject the cast.

I don't think you can do this in C++. Current GCC might grok it with
-fpermissive, but I haven't checked.
 
M

Melzzzzz

When I get a struct offset from a C style API as int. I.e.:

struct S
{ int I1;
double D1;
int I2;
};

int offset = offsetof(S, I2);

Now in C++ code I would like to convert this offset to a type safe
C++ member pointer of type int S::*. Older gcc versions simply
accepted a cast, newer version reject the cast.

What is safe about it? Implementation of member pointers is not
specified. It is far safer to just use offsetof.
 
J

Jorgen Grahn

Hi Marcel
%


I am curious, where do you use this offsetof
I mean in what situations are
you using it ?
I've been programming for 15+ years and it is the first time I see this
offsetof define, usually I just cast to the struct name and then address
it's members by their name, leaving the offset part to the compiler.

Would you be so kind and explain the need and give a couple of
small examples ?

Download the Linux kernel sources; it's used there. I can't remember
what for right now, but I think it was for container types or run-time
polymorphism, i.e. things supported without such hacks in C++.

/Jorgen
 
A

Asger Joergensen

Hi Drew

Drew said:
The main case I recall from back when I was using such things
involved runtime confirmation that a struct definition was correctly
overlaying a memory-mapped interface.

if ( offsetof(Interface, command_port) != 0x005 )
complain_that_world_is_wrong();

Looks like a very good purpose.

Thanks to all for explaining.

Best regards
Asger-P
http://Asger-P.dk/software
QLaunch, INI-Edit and Color Pick Pro.
 
C

Chris M. Thomasson

"Asger Joergensen" wrote in message
[...]
I am curious, where do you use this offsetof I mean in what situations
are
you using it ?

FWIW, I use it in C to get a compatible alignment of a type.

Something like:
_____________________________________________
#define ALIGN_OF(mp_type) \
offsetof( \
struct \
{ \
char pad_ALIGN_OF; \
mp_type type_ALIGN_OF; \
}, \
type_ALIGN_OF \
)
_____________________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top