Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/utility/enable_if/test/lazy.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.2 KB
Line 
1// Boost enable_if library
2
3// Copyright 2003 © The Trustees of Indiana University.
4
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)
8
9//    Authors: Jaakko Järvi (jajarvi at osl.iu.edu)
10//             Jeremiah Willcock (jewillco at osl.iu.edu)
11//             Andrew Lumsdaine (lums at osl.iu.edu)
12
13#include <boost/test/minimal.hpp>
14
15#include <boost/utility/enable_if.hpp>
16#include <boost/type_traits/is_same.hpp>
17
18using boost::enable_if_c;
19using boost::lazy_enable_if_c;
20
21// This class provides a reduced example of a traits class for
22// computing the result of multiplying two types.  The member typedef
23// 'type' in this traits class defines the return type of this
24// operator.  The return type member is invalid unless both arguments
25// for mult_traits are values that mult_traits expects (ints in this
26// case).  This kind of situation may arise if a traits class only
27// makes sense for some set of types, not all C++ types.
28
29template <class T> struct is_int {
30  BOOST_STATIC_CONSTANT(bool, value = (boost::is_same<T, int>::value));
31};
32
33template <class T, class U>
34struct mult_traits {
35  typedef typename T::does_not_exist type;
36};
37
38template <>
39struct mult_traits<int, int> {
40  typedef int type;
41};
42
43
44// Next, a forwarding function mult() is defined.  It is enabled only
45// when both arguments are of type int.  The first version, using
46// non-lazy enable_if_c does not work.
47
48#if 0
49template <class T, class U>
50typename enable_if_c<
51  is_int<T>::value && is_int<U>::value,
52  typename mult_traits<T, U>::type
53>::type
54mult(const T& x, const U& y) {return x * y;}
55#endif
56
57// A correct version uses lazy_enable_if_c.
58// This template removes compiler errors from invalid code used as an
59// argument to enable_if_c.
60
61#if 1
62template <class T, class U>
63typename lazy_enable_if_c<
64  is_int<T>::value & is_int<U>::value,
65  mult_traits<T, U> 
66>::type
67mult(const T& x, const U& y) {return x * y;}
68#endif
69
70double mult(int i, double d) { return (double)i * d; }
71
72int test_main(int, char*[])
73{
74
75
76  BOOST_CHECK(mult(1, 2) == 2);
77
78  BOOST_CHECK(mult(1, 3.0) == 3.0);
79
80  return 0;
81}
82
Note: See TracBrowser for help on using the repository browser.