Boost logo

Boost :

From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2007-10-04 15:57:23


Michael Marcin wrote:
> Phil Endecott wrote:
>> Joel de Guzman wrote:
>>> Phil Endecott wrote:
>>>> * xyz or [n] notation: using .x, .y and .z to access the coordinates
>>>> seems simpler to me, but the higher-dimension people would prefer to
>>>> use [0], [1] ... [n]. That notation also has the advantage that you
>>>> can iterate through the dimensions. Is it possible to support both?
>>> Yes! Use fusion to do the mapping, making all your structs, arrays or
>>> whatnots, whatever they are, compatible.
>>
>> I was imagining something more lightweight, like a union...
>>
>> There's also the possibility of simply defining implicit conversions
>> between the two styles.
>>
>> But a better solution would be to decide from the outset that one style
>> is "right" and the other is "wrong", so only one needs to be supported
>> and no conversions are needed. (I mean that partly tongue-in-cheek.)
>>
>
> Have a look at:
>
> http://www.gamedev.net/community/forums/topic.asp?topic_id=261920

Yes, a very good page showing several solutions:

1. A vector of member pointers (may have some overhead).
2. Anonymous structs and unions (may be non-standard).
3. x, y and z implemented as references (makes object bigger).
4. Relying on (&x)[1] pointing to y.

_IF_ 4 really is guaranteed to work portably (as its author claims), it
would get my vote:

template <typename T>
struct point2d {
   T x, y;
   T& operator[](int i) { return (&x)[i]; }
};

...what's the catch?

One complexity is how point2d and point3d types like this would
interoperate with a generic N-dimensional point:

template <typename T, int N>
struct point : public array<T,N> // or something like that

Is it possible specialise point<T,N> for N==2 and N==3 to add the xyz
stuff, and then typedef to the point2|3d names?

Phil.


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