Boost logo

Boost :

From: Michael Fawcett (michael.fawcett_at_[hidden])
Date: 2006-12-01 17:47:02


On 12/1/06, Michael Fawcett <michael.fawcett_at_[hidden]> wrote:
> On 12/1/06, Michael Marcin <mmarcin_at_[hidden]> wrote:
> >
> > glColor4fv( (float*)&red ); // make color red
> > glColor4fv( (float*)&red.xxxw() ); // make color white
> >
> > would this work? I'm guessing red would and white would not.
>
> You are correct. The actual type returned by red.xxxw() wold be
> vec4<float &>, although that just gave me an idea. What about
> overloading the address of operator when the underlying type is a
> reference? Something like (off the top of my head...not well thought
> out):

<snip untested code>

I just implemented it locally. There are probably better ways. In
particular I dislike the static variable, but I'm not sure how one
could do away with it. Ideas are welcome!

template <typename X, typename Y, typename Z, typename W>
struct vec4;

namespace detail
{

template <typename X, typename Y = X, typename Z = Y, typename W = Z>
struct address_of_dispatch
{
        X *operator()(vec4<X, Y, Z, W> &rhs) const { return rhs.array(); }
        const X *operator()(const vec4<X, Y, Z, W> &rhs) const { return rhs.array(); }
};
template <typename X, typename Y, typename Z, typename W>
struct address_of_dispatch<X &, Y &, Z &, W &>
{
        X *operator()(vec4<X &, Y &, Z &, W &> &rhs) const
        {
                static vec4<X, Y, Z, W> nrv;
                nrv = rhs;
                return nrv.array();
        }
        const X *operator()(const vec4<X &, Y &, Z &, W &> &rhs) const
        {
                return (*this)(const_cast<vec4<X &, Y &, Z &, W &> &>(rhs));
        }
};

}

// Inside vec4's definition
typename boost::remove_reference<X>::type *operator&(void)
{
        return detail::address_of_dispatch<X, Y, Z, W>()(*this);
}
typename boost::remove_reference<X>::type const *operator&(void) const
{
        return detail::address_of_dispatch<X, Y, Z, W>()(*this);
}

That works...whether it's the best solution is another story...

I'll update the Vault files to include these changes. Thanks for
bringing this issue up!

--Michael Fawcett


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