-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest4.cpp
115 lines (94 loc) · 2.96 KB
/
test4.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// ************************************************************************** //
// //
// ::: :::::::: //
// test4.cpp :+: :+: :+: //
// +:+ +:+ +:+ //
// By: ngoguey <[email protected]> +#+ +:+ +#+ //
// +#+#+#+#+#+ +#+ //
// Created: 2015/11/26 12:08:05 by ngoguey #+# #+# //
// Updated: 2015/11/26 12:17:02 by ngoguey ### ########.fr //
// //
// ************************************************************************** //
#include <iostream>
#include <type_traits>
#include <vector>
#include <list>
#define ISSAME(A, B) std::is_same<A, B>::value
#define OK_IF(PRED) typename std::enable_if<PRED>::type* = nullptr
template<typename T>
struct has_iterator
{
private:
typedef char yes_t[1];
typedef char no_t[2];
template<typename C>
static yes_t &test(typename C::const_iterator*);
template<typename C>
static no_t &test(...);
using ItVal = decltype(test<T>(nullptr));
public:
static constexpr bool value = ISSAME(ItVal, yes_t&);
};
template<typename T >
class has_begin
{
typedef char yes_t[1];
typedef char no_t[2];
template <class C
, class WishedFun = typename C::const_iterator (C::*)() const
, class BeginFun = decltype(static_cast<WishedFun>(&C::begin))
, OK_IF(ISSAME(WishedFun, BeginFun))
>
static yes_t &test(void *);
template <class C>
static no_t &test(...);
using BeginVal = decltype(test<T>(nullptr));
public:
static constexpr bool value = ISSAME(BeginVal, yes_t&);
};
template<typename T >
class has_end
{
typedef char yes_t[1];
typedef char no_t[2];
template <class C
, class WishedFun = typename C::const_iterator (C::*)() const
, class EndFun = decltype(static_cast<WishedFun>(&C::end))
, OK_IF(ISSAME(WishedFun, EndFun))
>
static yes_t &test(void *);
template <class C>
static no_t &test(...);
using EndVal = decltype(test<T>(nullptr));
public:
static constexpr bool value = ISSAME(EndVal, yes_t&);
};
template<typename T>
struct is_container : std::integral_constant<
bool, has_iterator<T>::value
&& has_begin<T>::value && has_end<T>::value>
{};
int main(void)
{
std::cout
<< "beg"
<< has_begin< std::vector<int> >::value
<< " end"
<< has_end< std::vector<int> >::value
<< " it"
<< has_iterator< std::vector<int> >::value
<< " container"
<< is_container< std::vector<int> >::value
<< std::endl;
std::cout
<< "beg"
<< has_begin< int >::value
<< " end"
<< has_end< int >::value
<< " it"
<< has_iterator< int >::value
<< " container"
<< is_container< int >::value
<< std::endl;
return (0);
}