Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2004-12-02 10:05:27


David Abrahams wrote:
> David Abrahams wrote:
>
>> Still no dice for vc7.1, though.
>
>
> Hum, that was too easy:
>

For every difficult problem, there is a solution that is simple, elegant
... and wrong. Correct version below fails. :( You can't use int for
this test because the "const" is ignored in "int const const_rvalue()".
You need to use UDTs.

#include <boost/static_assert.hpp>

typedef char yes;
typedef char (&no)[2];

template <class T>
yes is_nonconst_rvalue(T const&, ...);

template <class T>
no is_nonconst_rvalue(T&, int);

struct foo
{
#ifndef BOOST_MSVC
     template<typename U>
         operator U() const;

     template<typename V>
         operator V& () volatile const;
#else
     template<typename U>
         operator U() volatile;

     template<typename V>
         operator V& () const;
#endif

     static foo const instance;
     static bool select;
};

#define IS_RVALUE(x) \
     (sizeof(is_nonconst_rvalue((foo::select ? foo::instance : (x)), 0))
== sizeof(yes))

struct bar {};

bar rvalue();
bar const const_rvalue();
bar& lvalue();
bar const& const_lvalue();

bar volatile volatile_rvalue();
bar const volatile const_volatile_rvalue();
bar volatile& volatile_lvalue();
bar const volatile& const_volatile_lvalue();

BOOST_STATIC_ASSERT(IS_RVALUE(rvalue()));
BOOST_STATIC_ASSERT(IS_RVALUE(const_rvalue()));
BOOST_STATIC_ASSERT(!IS_RVALUE(lvalue()));
BOOST_STATIC_ASSERT(!IS_RVALUE(const_lvalue()));

BOOST_STATIC_ASSERT(IS_RVALUE(volatile_rvalue()));
BOOST_STATIC_ASSERT(IS_RVALUE(const_volatile_rvalue()));
BOOST_STATIC_ASSERT(!IS_RVALUE(volatile_lvalue()));
BOOST_STATIC_ASSERT(!IS_RVALUE(const_volatile_lvalue()));

-- 
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