Boost logo

Boost :

From: Bruno Lalande (bruno.lalande_at_[hidden])
Date: 2008-05-03 07:38:58


> >As pointed out by John, another advantage of compile-time access is
> >the ability to have different types for each coordinate. It's
> >something that has been asked for several times during recent
> >discussions on this list. I was even wondering the other day if it
> >wouldn't be better to not require any coordinate_type typedef and have
> >the algorithms deducing by themselves the type of each coordinate by a
> >BOOST_TYPEOF on the accessor.
>
> Can you provide me with a code example of how to do this? I'm getting
> really tired of writing typename point_traits<T>::coordinate_type over
> and over and over again. I would greatly appreciate a better way.

You can have the type of a coordinate by applying BOOST_TYPEOF on the
expression by which you access this coordinate. For instance, let's
say we have a tuple<float,double> t:

BOOST_TYPEOF(t.get<0>()) x = t.get<0>();
BOOST_TYPEOF(t.get<1>()) y = t.get<1>();

x will be a float and y will be a double. Here you can see that we
ended up writing "t.get<0>()" and "t.get<1>()" twice, once for the
declaration and once for the initialization. BOOST_AUTO can help by
both declaring and assigning at the same time:

BOOST_AUTO(x, t.get<0>());
BOOST_AUTO(y, t.get<1>());

x is a float and receives the value of t.get<0>(), y is a double and
receives the value of t.get<1>().

Bruno


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