lazy corrections

[SVN r1730]
This commit is contained in:
Jaakko Järvi
2003-11-03 20:45:20 +00:00
parent a5c7239fa9
commit 2c98ab4ac8
2 changed files with 43 additions and 33 deletions

View File

@@ -11,64 +11,68 @@
#include <boost/test/minimal.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
#include <boost/type_traits/is_same.hpp>
using boost::enable_if_c;
using boost::lazy_enable_if_c;
using boost::is_arithmetic;
// This class provides a reduced example of a traits class for computing
// the result of multiplying two types. The exists constant is true when a
// multiplication operator exists between two types, and the type member is
// defined to the return type of this operator. The return type member is
// not defined when a multiplication operator does not exist.
// This class provides a reduced example of a traits class for
// computing the result of multiplying two types. The member typedef
// 'type' in this traits class defines the return type of this
// operator. The return type member is invalid unless both arguments
// for mult_traits are values that mult_traits expects (ints in this
// case). This kind of situation may arise if a traits class only
// makes sense for some set of types, not all C++ types.
template <class T> struct is_int {
BOOST_STATIC_CONSTANT(bool, value = (boost::is_same<T, int>::value));
};
template <class T, class U>
struct mult_traits {
BOOST_STATIC_CONSTANT(bool, exists = false);
typedef typename T::does_not_exist type;
};
template <>
struct mult_traits<int, int> {
BOOST_STATIC_CONSTANT(bool, exists = true);
typedef int type;
};
template <>
struct mult_traits<double, double> {
BOOST_STATIC_CONSTANT(bool, exists = true);
typedef double type;
};
// Next, a forwarding function mult() is defined. It is enabled only when
// mult_traits<T, U>::exists is true. The first version, using normal
// enable_if_c, works with only some compilers (it does not work in g++):
// Next, a forwarding function mult() is defined. It is enabled only
// when both arguments are of type int. The first version, using
// non-lazy enable_if_c does not work.
#if 0
template <class T, class U>
typename enable_if_c<
mult_traits<T, U>::exists,
typename mult_traits<T, U>::type>::type
is_int<T>::value && is_int<U>::value,
typename mult_traits<T, U>::type
>::type
mult(const T& x, const U& y) {return x * y;}
#endif
// An improved version that works with more compilers uses lazy_enable_if_c.
// A correct version uses lazy_enable_if_c.
// This template removes compiler errors from invalid code used as an
// argument to enable_if_c.
#if 1
template <class T, class U>
typename lazy_enable_if_c<
mult_traits<T, U>::exists,
mult_traits<T, U> >::type
is_int<T>::value && is_int<U>::value,
mult_traits<T, U>
>::type
mult(const T& x, const U& y) {return x * y;}
#endif
double mult(int i, double d) { return (double)i * d; }
int test_main(int, char*[])
{
BOOST_CHECK(mult(1, 2) == 2);
BOOST_CHECK(mult(1.0, 3.0) == 3.0);
BOOST_CHECK(mult(1, 3.0) == 3.0);
return 0;

View File

@@ -8,11 +8,13 @@
// Andrew Lumsdaine (lums at osl.iu.edu)
//
// Testing all variations of lazy_enable_if.
#include <boost/test/minimal.hpp>
#include <boost/mpl/not.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
#include <boost/type_traits/is_same.hpp>
using boost::lazy_enable_if;
using boost::lazy_disable_if;
@@ -20,22 +22,26 @@ using boost::lazy_disable_if;
using boost::lazy_enable_if_c;
using boost::lazy_disable_if_c;
using boost::is_arithmetic;
template <class T>
struct is_int_or_double {
BOOST_STATIC_CONSTANT(bool,
value = (boost::is_same<T, int>::value ||
boost::is_same<T, double>::value));
};
template <class T>
struct some_traits {
BOOST_STATIC_CONSTANT(bool, value = false);
typedef typename T::does_not_exist type;
};
template <>
struct some_traits<int> {
BOOST_STATIC_CONSTANT(bool, value = true);
typedef bool type;
};
template <>
struct some_traits<double> {
BOOST_STATIC_CONSTANT(bool, value = true);
typedef bool type;
};
@@ -53,21 +59,21 @@ struct make_bool<double> {};
namespace A {
template<class T>
typename lazy_enable_if<some_traits<T>, some_traits<T> >::type
typename lazy_enable_if<is_int_or_double<T>, some_traits<T> >::type
foo(T t) { return true; }
template<class T>
typename lazy_enable_if_c<some_traits<T>::value, some_traits<T> >::type
typename lazy_enable_if_c<is_int_or_double<T>::value, some_traits<T> >::type
foo2(T t) { return true; }
}
namespace B {
template<class T>
typename lazy_disable_if<some_traits<T>, make_bool<T> >::type
typename lazy_disable_if<is_int_or_double<T>, make_bool<T> >::type
foo(T t) { return false; }
template<class T>
typename lazy_disable_if_c<some_traits<T>::value, make_bool<T> >::type
typename lazy_disable_if_c<is_int_or_double<T>::value, make_bool<T> >::type
foo2(T t) { return false; }
}