|
Boost : |
From: David Abrahams (abrahams_at_[hidden])
Date: 2000-08-25 14:36:38
That's why you use this construct instead:
template <class PodT>
class PodAlignment
{
private:
struct Aligner { char misalign; PodT x; };
public:
enum { value = offsetof(Aligner, x) };
};
Of course there are no guarantees when PodT is non-pod, but Valentin was
asking about *real* cases...
-Dave
----- Original Message -----
From: "Bill Wade" <bill.wade_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, August 25, 2000 12:18 PM
Subject: RE: [boost] low-level sub-allocation/alignment challenge
> > From: Valentin Bonnard [mailto:Bonnard.V_at_[hidden]]
>
> > Are the any real cases where offsetof doesn't work on non PODs (except
> > with virtual bases of course) ?
>
> It can easily be too pessimistic
>
> struct foo
> {
> compiler:
> __foo_vtable* __vptr; // Perhaps four bytes
> public:
> char c;
> T t;
> virtual void bar();
> };
>
> If the correct alignment for T is four bytes, offsetof(foo,t) is probably
> eight. offsetof(foo,t)-offset(foo,c) gives the right answer in this case,
> but if the alignment of T is eight bytes the subtraction becomes too
> optimistic.
>
>
>
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk