Boost logo

Boost :

From: Eric Friedman (ebf_at_[hidden])
Date: 2002-07-02 00:00:34


Any ideas on how to make the following traits template work? It would be
greatly useful to boost::variant (and probably other applications as
well).

Essentially, is_better_conversion should have the following
(well-defined) behavior:

 #1: value = true if type To1 is a better conversion than type To2
 #2: value = false if type To2 is a better conversion than type To1
 #3: value = false if neither conversion is better (i.e. conversion is
ambiguous)

Thanks,
Eric

---
typedef ::boost::type_traits::no_type better_To1;
typedef ::boost::type_traits::yes_type better_To2;
template <typename From, typename To1, typename To2>
struct is_better_conversion
{
private:
	static better_To1 BOOST_TT_DECL check(To1);
	static better_To2 BOOST_TT_DECL check(To2);
	static From m_from;
public:
	BOOST_STATIC_CONSTANT(bool,
		value = (sizeof(check(m_from)) == sizeof(better_To1)));
};
int main()
{
  // Cases #1 and #2 work exactly as intended:
  BOOST_STATIC_ASSERT((is_better_conversion<float, double,
int>::value));
  BOOST_STATIC_ASSERT((!is_better_conversion<float, int,
double>::value));
  // Case #3 currently results in ambiguous is_better_conversion::check
overload;
  // it would be *very* nice if the result was simply 'false' in this
case:
  BOOST_STATIC_ASSERT((!is_better_conversion<double, float,
int>::value));
}

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