1 
2 /*
3  *
4  * (C) Copyright John Maddock 1999-2005.
5  * Use, modification and distribution are subject to the
6  * Boost Software License, Version 1.0. (See accompanying file
7  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8  *
9  * This file provides some example of type_traits usage -
10  * by "optimising" various algorithms:
11  *
12  * opt::iter_swap - uses type_traits to determine whether the iterator is a proxy
13  *                  in which case it uses a "safe" approach, otherwise calls swap
14  *                  on the assumption that swap may be specialised for the pointed-to type.
15  *
16  */
17 
18 #include <iostream>
19 #include <typeinfo>
20 #include <algorithm>
21 #include <iterator>
22 #include <vector>
23 #include <memory>
24 
25 #include <boost/test/included/prg_exec_monitor.hpp>
26 #include <boost/type_traits.hpp>
27 
28 using std::cout;
29 using std::endl;
30 using std::cin;
31 
32 namespace opt{
33 
34 //
35 // iter_swap:
36 // tests whether iterator is a proxy iterator or not, and
37 // uses optimal form accordingly:
38 //
39 namespace detail{
40 
41 template <typename I>
do_swap(I one,I two,const boost::false_type &)42 static void do_swap(I one, I two, const boost::false_type&)
43 {
44    typedef typename std::iterator_traits<I>::value_type v_t;
45    v_t v = *one;
46    *one = *two;
47    *two = v;
48 }
49 template <typename I>
do_swap(I one,I two,const boost::true_type &)50 static void do_swap(I one, I two, const boost::true_type&)
51 {
52    using std::swap;
53    swap(*one, *two);
54 }
55 
56 }
57 
58 template <typename I1, typename I2>
iter_swap(I1 one,I2 two)59 inline void iter_swap(I1 one, I2 two)
60 {
61    //
62    // See is both arguments are non-proxying iterators,
63    // and if both iterator the same type:
64    //
65    typedef typename std::iterator_traits<I1>::reference r1_t;
66    typedef typename std::iterator_traits<I2>::reference r2_t;
67 
68    typedef boost::integral_constant<bool,
69       ::boost::is_reference<r1_t>::value
70       && ::boost::is_reference<r2_t>::value
71       && ::boost::is_same<r1_t, r2_t>::value> truth_type;
72 
73    detail::do_swap(one, two, truth_type());
74 }
75 
76 
77 };   // namespace opt
78 
cpp_main(int argc,char * argv[])79 int cpp_main(int argc, char* argv[])
80 {
81    //
82    // testing iter_swap
83    // really just a check that it does in fact compile...
84    std::vector<int> v1;
85    v1.push_back(0);
86    v1.push_back(1);
87    std::vector<bool> v2;
88    v2.push_back(0);
89    v2.push_back(1);
90    opt::iter_swap(v1.begin(), v1.begin()+1);
91    opt::iter_swap(v2.begin(), v2.begin()+1);
92 
93    return 0;
94 }
95 
96 
97 
98