Boost logo

Boost :

From: Gavin Collings (gcollings_at_[hidden])
Date: 2000-01-07 06:39:01

> > Oh, here's another feature I like for my points: indexability (I like
> > to be able to access the coordinates by index). That allows
> > programmatic selection of orientation for things like scrollbars.
> > Indexability is of course unusable when the types of x and y differ.
> > OTOH this is definitely a corner case. I have no strong opinion on the
> > topic.
> We can provide generic version of classes with two template parameters
> (and without indexability) and a specialization for case T1==T2 with
> operator[]. I think it will be reasonable solution.

I have some sympathy with the overdesign comment, but it may be worthwhile
considering the following for the generic version. It overloads the [] operator
and uses function overloading to achieve programmatic selection on orientation
etc. Obviously, as it's a compile time thing, it can't be used for looping over
(x .. y) - but that shouldn't be a problem for just two coordinates.

    struct x_coord {} x;
    struct y_coord {} y;

    template <typename X, typename Y = X> class point

       X operator[]( x_coord ) { return x_; }
       Y operator[]( y_coord ) { return y_; }


       X x_;
       Y y_;

    class latitude {};
    class longitude {};

    void process_coord( latitude ) {}
    void process_coord( longitude ) {}

    void test()
       point<latitude, longitude> p;

       latitude lat = p[x];
       longitude lon = p[y];

       process_coord( p[x] );
       process_coord( p[y] );


Gavin Collings

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