-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagdispatch.cpp
62 lines (54 loc) · 1.02 KB
/
tagdispatch.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
/*
Tag dispatch technique, an alternative to SFINAE
https://ideone.com/1vCjmu
*/
#include <iostream>
class Container{};
class DC1 : public Container {};
class DC2 : public DC1 {};
class Other {};
namespace kk
{
template <typename T>
struct is_container : std::false_type {};
template <>
struct is_container<Container> : std::true_type {};
template <>
struct is_container<DC1> : std::true_type {};
template <>
struct is_container<DC2> : std::true_type {};
}
// or
namespace kk2
{
template <typename T>
struct is_container : std::is_base_of<Container, T> {};
}
namespace detail
{
template <typename T>
bool isCont(T const &in, std::true_type)
{
std::cout << "is container\n";
return true;
}
template <typename T>
bool isCont(T const &in, std::false_type)
{
std::cout << "is not a container\n";
return false;
}
}
template <typename T>
bool isCont(T const &in)
{
return detail::isCont(in, kk2::is_container<T>{});
}
int main()
{
isCont(Container{});
isCont(DC1{});
isCont(DC2{});
isCont(Other{});
return 0;
}