Boost logo

Boost :

From: Pavel Kuznetsov (pavel_at_[hidden])
Date: 2004-08-24 09:48:56


David,

>> Here it is.

> Looks way too heavy to me for what it's accomplishing.
> How about:
> namespace error
> {
> template <class T, class Value>
> int
> dereferenceable_requires_lvalue_dereference(Value&);
> }
> template <class T, class P, class B =
> ::boost::detail::empty_base>
> struct dereferenceable : B
> {
> P operator->() const
> {
> enum
> {
> assertion = sizeof(
> error::dereferenceable_requires_lvalue_dereference<T>(
> *static_cast<const T&>(*this)
> ))
> };
> return &*static_cast<const T&>(*this);
> }
> };
> ??
> Does that work?

Alas, that does not, since at least MSVC allows binding rvalue
to non-const reference. But we can simplify original approach
along these lines:

namespace error
{
   template <class T, class Value>
   int
   dereferenceable_requires_lvalue_dereference(
     Value& (T::*)()const);
}

template <class T, class P, class B =
   ::boost::detail::empty_base>
struct dereferenceable : B
{
    P operator->() const
    {
      enum
      {
        assertion = sizeof(
          error::dereferenceable_requires_lvalue_dereference<T>(
            &T::operator*
          ))
      };
      return &*static_cast<const T&>(*this);
    }
};

-- 
Pavel Kuznetsov
MetaCommunications Engineering
http://www.meta-comm.com/engineering

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