Connecting Tech Pros Worldwide Forums | Help | Site Map

Offset using pointer to member

Dave
Guest
 
Posts: n/a
#1: Jul 23 '05

Hello all,

For purposes of understanding the language better, I tried to determine the
offset of a struct member by using pointers to members (instead of something
such as the offsetof macro). However, if I understand correctly, this may
not be done portably. If I try to print a pointer to member, it just
converts to bool and I always get 1 (unless it's a null pointer to member).
There are no conversions from pointer to member to an integral type, so I
think I'm out of luck. Of course, I could hack the underlying
representation on my platform, but I want to be Standard-compliant...

Does anybody know of a way I can do what I want, or is my analysis correct
that it cannot be done portably?

Thanks,
Dave


Gianni Mariani
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Offset using pointer to member


Dave wrote:[color=blue]
> Hello all,
>
> For purposes of understanding the language better, I tried to determine the
> offset of a struct member by using pointers to members (instead of something
> such as the offsetof macro). However, if I understand correctly, this may
> not be done portably. If I try to print a pointer to member, it just
> converts to bool and I always get 1 (unless it's a null pointer to member).
> There are no conversions from pointer to member to an integral type, so I
> think I'm out of luck. Of course, I could hack the underlying
> representation on my platform, but I want to be Standard-compliant...
>
> Does anybody know of a way I can do what I want, or is my analysis correct
> that it cannot be done portably?[/color]


It's ugly but this should work.


template <typename T, typename V>
inline int MemberOffsetOf( V T::* v )
{
return ((int)&((( A* )0)->*v));
}



As for a compile-time version, you can't do it because you need to use
the "&" operator (address of) and this is apparently illegal in a
constant expression.

This is somthing like what it would look like for a compile-time
constant had the "&" operator been allowed.


template <typename T, typename V>
struct ClassValue
{
typedef T classtype;
typedef V valuetype;


template < V T::* P >
struct DoOffset
{
// & illegal here - sniff sniff ...
static const int offset = ( (int) & ( ( (T*) 0)->*P ) );
};


template <V T::* P>
char ( & SizeofThing() )[ DoOffset<P>::offset ];
};




template <typename T, typename V>
ClassValue< T, V > SizeofHelper( V T::* );


#define MemberOffsetOf( Member ) \
sizeof( SizeofHelper( Member ).SizeofThing< Member >() )


Jerry Coffin
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Offset using pointer to member


> It's ugly but this should work.[color=blue]
>
> template <typename T, typename V>
> inline int MemberOffsetOf( V T::* v )
> {
> return ((int)&((( A* )0)->*v));
> }[/color]

Emphasis on _should_ here -- even though you and I know it's really
only doing compile-time math, this is still officially dereferencing a
null pointer, so officially it gives undefined behavior.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Dave
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Offset using pointer to member



"Jerry Coffin" <jcoffin@taeus.com> wrote in message
news:1105326404.275113.22180@c13g2000cwb.googlegro ups.com...[color=blue][color=green]
> > It's ugly but this should work.
> >
> > template <typename T, typename V>
> > inline int MemberOffsetOf( V T::* v )
> > {
> > return ((int)&((( A* )0)->*v));
> > }[/color]
>
> Emphasis on _should_ here -- even though you and I know it's really
> only doing compile-time math, this is still officially dereferencing a
> null pointer, so officially it gives undefined behavior.
>
> --
> Later,
> Jerry.
>
> The universe is a figment of its own imagination.
>[/color]

Good point here! But interestingly, that same comment would also be true of
the way the C macro offsetof is typically implemented!


Jerry Coffin
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Offset using pointer to member


> Good point here! But interestingly, that same comment would also be
true of[color=blue]
> the way the C macro offsetof is typically implemented![/color]

That's true, but a bit different -- it's part of the implementation,
and the implementor is free to do things that only work on that
particular implementation.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Closed Thread