Boost logo

Boost :

From: Geoffrey Irving (irving_at_[hidden])
Date: 2006-09-25 00:59:10


On Sat, Sep 23, 2006 at 04:53:00PM +0100, Dave Harris wrote:
> In-Reply-To: <20060922201042.GO21158_at_[hidden]>
> irving_at_[hidden] (Geoffrey Irving) wrote (abridged):
> > Especially if you can make it a static assert (which I'm not sure of).
>
> Making it a static assert is not straightforward because addresses are not
> compile-time constants. The straightforward way to compute offsets is by
> subtracting addresses.

I wasn't asking whether it was straightforward, just possible.
This should work:

template<class T> class vectorN
{
public:
    T x1, x2, ..., xN;

    T& operator[](unsigned int i)
    {assert(i<Nu);return (&x1)[i];}

private:
    struct checker:public vectorN
    {
        T padding[256-N];
    }

    BOOST_STATIC_ASSERT((sizeof(checker)==256*sizeof(T)));
};

Since elements of arrays are required to be in contiguous memory locations,
the variables x1 through xN must also be contigous if the static assert
passes. I don't know if this is a necessary condition, but it's certainly
sufficient.

Here I'm assuming that the compiler aligns to at most 256 bytes.

Geoffrey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk