Files
boost_utility/test/enable_if_lazy.cpp

76 lines
2.0 KiB
C++
Raw Normal View History

2003-07-02 21:34:25 +00:00
//
// Copyright 2003 <20> The Trustees of Indiana University. All rights
// reserved.
//
// See the file enable_if_LICENSE for licensing conditions.
//
// Authors: Jaakko J<>rvi (jajarvi@osl.iu.edu)
// Jeremiah Willcock (jewillco@osl.iu.edu)
// Andrew Lumsdaine (lums@osl.iu.edu)
//
#include <boost/test/minimal.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
2003-09-05 18:22:13 +00:00
using boost::enable_if_c;
using boost::lazy_enable_if_c;
2003-07-02 21:34:25 +00:00
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.
template <class T, class U>
struct mult_traits {
BOOST_STATIC_CONSTANT(bool, exists = false);
};
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
2003-09-05 18:22:13 +00:00
// enable_if_c, works with only some compilers (it does not work in g++):
2003-07-02 21:34:25 +00:00
#if 0
template <class T, class U>
2003-09-05 18:22:13 +00:00
typename enable_if_c<
2003-07-02 21:34:25 +00:00
mult_traits<T, U>::exists,
typename mult_traits<T, U>::type>::type
mult(const T& x, const U& y) {return x * y;}
#endif
2003-09-05 18:22:13 +00:00
// An improved version that works with more compilers uses lazy_enable_if_c.
2003-07-02 21:34:25 +00:00
// This template removes compiler errors from invalid code used as an
2003-09-05 18:22:13 +00:00
// argument to enable_if_c.
2003-07-02 21:34:25 +00:00
template <class T, class U>
2003-09-05 18:22:13 +00:00
typename lazy_enable_if_c<
2003-07-02 21:34:25 +00:00
mult_traits<T, U>::exists,
mult_traits<T, U> >::type
mult(const T& x, const U& y) {return x * y;}
2003-09-05 18:22:13 +00:00
2003-07-02 21:34:25 +00:00
int test_main(int, char*[])
{
BOOST_TEST(mult(1, 2) == 2);
BOOST_TEST(mult(1.0, 3.0) == 3.0);
return 0;
}