-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathles05_SFINAE_enable_if.cpp
84 lines (72 loc) · 1.86 KB
/
les05_SFINAE_enable_if.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <list>
template <typename T1, typename T2>
struct is_equal : std::false_type
{
};
// specialization
template <typename T>
struct is_equal<T, T> : std::true_type
{
};
template <typename Iterator>
struct is_random_access_iterator
: is_equal<
typename std::iterator_traits<Iterator>::iterator_category,
std::random_access_iterator_tag
>
{
};
template <bool flag, typename T = void>
struct enable_if
{
};
//if the arguement is true we get an embedded typedef
template <typename T>
struct enable_if<true, T>
{
typedef T type;
};
template <typename Iter>
typename enable_if<!is_random_access_iterator<Iter>::value>::type
my_advance(Iter& it, int n)
{
while(n--)
++it;
//diagnostic print
std::cout << " my_advance used loop; it did not have a random_access_iterator"
<< std::endl;
}
template <typename Iter>
typename enable_if<is_random_access_iterator<Iter>::value>::type
my_advance(Iter& it, int n)
{
it += n;
//diagnostic print
std::cout << " my_advance used const; it had a random_access_iterator"
<< std::endl;
}
template <typename Container>
bool is_palindrome(Container s)
{
auto middle = s.begin();
//std::advance( middle, s.size() / 2 );
my_advance( middle, s.size() / 2 );
return std::equal( s.begin(), middle, s.rbegin() );
}
int main()
{
std::vector<char> l = {'m', 'a', 'd', 'a', 'm'};
std::cout << " palindrome = " << is_palindrome(l) << ", ";
for(auto& i : l) std::cout << i ; // print the vector as a word
std::cout << std::endl;
std::list<char> t = {'t', 'e', 's', 't'};
std::cout << " palindrome = " << is_palindrome(t) << ", ";
for(auto& i : t) std::cout << i ; // print the list as a word
std::cout << std::endl;
return 0;
}