Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2005-02-26 15:10:41


Peder Holt wrote:
> I was looking through the FOR_EACH - implementation, and noticed that
> it lacks rvalue detection for Visual C++ 6.0.

True, because a Microsoft "extension" allows rvalues to bind to
non-const references.

> After some investigation, I have found one very sleazy way of
> detecting rvalue-ness runtime without evaluating the argument more
> than once:
>
> # define BOOST_FOREACH_TYPEOF(COL)
> \
> *(true ? 0 : &::boost::for_each::wrap(COL))

This is dereferencing a null pointer!

>
> template<typename T>
> inline static_any<simple_variant<T const> > contain(int const&
> position_test,T const &t, bool const &, mpl::false_)
> {
> int rvalue_test=int(&t)-int(&position_test);
> bool rvalue=(rvalue_test<=8);
>
> return rvalue ? simple_variant<T const>(t) : simple_variant<T const>(&t);
> }
>
> Since this relies on the position of objects on the stack, there are
> probably cases where it will fail.
> Pointers (e.g. string constants) must be handled separately.
>

I think I understand what you are trying to do, but it won't work in
general. You are assuming that an rvalue's address will always have the
same relation to an lvalue's address. But the lvalue could be in
automatic storage (on the stack), in dynamic storage (on the heap) or in
static storage (eg. a global object, possibly in another DLL). Besides
the fact that comparing addresses in this way is bad karma, are you
certain that all the above cases (automatic, dynamic, static) are handled?

I'd rather wait until a better approach is found.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

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