Boost logo

Boost :

From: Dave Harris (brangdon_at_[hidden])
Date: 2006-09-21 15:15:26

In-Reply-To: <20060921173331.GJ14139_at_[hidden]>
irving_at_[hidden] (Geoffrey Irving) wrote (abridged):
> I don't think there's any reason to overthink this. The simple scheme
> works:
> template<class T> class vector3
> {
> public:
> T x,y,z;
> vector3()
> {
> assert(&z == &x+3);
> }
> T& operator[](unsigned i)
> {assert(i<3);return (&x)[i];}
> };
> It can probably even be considered standard compliant: if the assert
> doesn't hit, I'm pretty sure the standard guarantees that the code
> is correct.

You probably meant "&x+2" in the first assert. I agree if that assert
passes the code probably works, but I don't think the assert is guaranteed
to pass. Personally I don't think code like that belongs in Boost; Boost
ought to be exemplary.

I'd prefer:

 template<class T> class vector3 {
     T base[3];
     T& operator[](unsigned i) {
         return base[i];
     T& x() { return base[0]; }
     T& y() { return base[1]; }
     T& z() { return base[2]; }

with no public variables. The encapsulation is worth the extra brackets.
(Actually I'd rather use v.set_x(3) than v.x() = 3.)

-- Dave Harris, Nottingham, UK.

Boost list run by bdawes at, gregod at, cpdaniel at, john at