Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-01-31 14:46:12


Take two on the g++ fix:

We need to find something to replace the use of elipses to catch the
non-convertible case. I propose we use the following class instead.

namespace detail {
  struct accept_any {
    template <class T>
    accept_any(const T&) { }
  };
}

There's a problem with using accept_any. When the From type has a
user-defined implicit conversion operator there will be ambiguity between
the calls to the two check functions. So the following won't work:

template <class From, class To>
struct is_convertible
{
private:
 typedef char (&no)[1];
 typedef char (&yes)[2];

 template <class T>
 struct checker
 {
   static no check(detail::accept_any);
   static yes check(T);
 };
 static From from;
public:
 static const bool value =
   sizeof( checker<To>::check(from) ) == sizeof(yes);
};

However, I think this can be overcome by putting in a bias for the "yes"
check function. To do this I add another argument to the check functions.
The "no" check function gets another accept_any argument, while the "yes"
check function gets From as the second argument, thereby making it a
better fit in the case when there is a conversion.

template <class From, class To>
struct is_convertible
{
private:
 typedef char (&no)[1];
 typedef char (&yes)[2];

 template <class T>
 struct checker
 {
   static no check(detail::accept_any, detail::accept_any);
   static yes check(T, From);
 };
 static From from;
public:
 static const bool value =
   sizeof( checker<To>::check(from, from) ) == sizeof(yes);
};

So, is my logic here OK? I've rerun the type traits test and it seems to
work.

Cheers,
Jeremy

On Wed, 31 Jan 2001, David Abrahams wrote:
abraha> Jeremy,
abraha>
abraha> I don't believe that this "fix" works. I think you forgot about user-defined
abraha> implicit conversions (i.e. conversion operators and non-explicit
abraha> constructors taking a single argument).
abraha>
abraha> Also, I think I mentioned that now CodeWarrior reports:
abraha> is_convertible<std::random_access_iterator_tag,
abraha> std::random_access_iterator_tag>::value is false.
abraha>
abraha> -Dave

----------------------------------------------------------------------
 Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
 Ph.D. Candidate email: jsiek_at_[hidden]
 Univ. of Notre Dame work phone: (219) 631-3906
----------------------------------------------------------------------


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