1 // Boost enable_if library
3 // Copyright 2003 (c) The Trustees of Indiana University.
5 // Use, modification, and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // Authors: Jaakko Jarvi (jajarvi at osl.iu.edu)
10 // Jeremiah Willcock (jewillco at osl.iu.edu)
11 // Andrew Lumsdaine (lums at osl.iu.edu)
13 #include <boost/utility/enable_if.hpp>
14 #include <boost/type_traits/is_same.hpp>
15 #include <boost/detail/lightweight_test.hpp>
17 using boost::enable_if_c
;
18 using boost::lazy_enable_if_c
;
20 // This class provides a reduced example of a traits class for
21 // computing the result of multiplying two types. The member typedef
22 // 'type' in this traits class defines the return type of this
23 // operator. The return type member is invalid unless both arguments
24 // for mult_traits are values that mult_traits expects (ints in this
25 // case). This kind of situation may arise if a traits class only
26 // makes sense for some set of types, not all C++ types.
28 template <class T
> struct is_int
{
29 BOOST_STATIC_CONSTANT(bool, value
= (boost::is_same
<T
, int>::value
));
32 template <class T
, class U
>
34 typedef typename
T::does_not_exist type
;
38 struct mult_traits
<int, int> {
43 // Next, a forwarding function mult() is defined. It is enabled only
44 // when both arguments are of type int. The first version, using
45 // non-lazy enable_if_c does not work.
48 template <class T
, class U
>
50 is_int
<T
>::value
&& is_int
<U
>::value
,
51 typename mult_traits
<T
, U
>::type
53 mult(const T
& x
, const U
& y
) {return x
* y
;}
56 // A correct version uses lazy_enable_if_c.
57 // This template removes compiler errors from invalid code used as an
58 // argument to enable_if_c.
61 template <class T
, class U
>
62 typename lazy_enable_if_c
<
63 is_int
<T
>::value
& is_int
<U
>::value
,
66 mult(const T
& x
, const U
& y
) {return x
* y
;}
69 double mult(int i
, double d
) { return (double)i
* d
; }
75 BOOST_TEST(mult(1, 2) == 2);
77 BOOST_TEST(mult(1, 3.0) == 3.0);
79 return boost::report_errors();