- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtagdispatch.cpp
More file actions
Latest commit
62 lines (54 loc) · 1.02 KB
/
Copy pathtagdispatch.cpp
File metadata and controls
62 lines (54 loc) · 1.02 KB
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>
classContainer{};
classDC1 : publicContainer {};
classDC2 : publicDC1 {};
classOther {};
namespacekk
{
template <typename T>
structis_container : std::false_type {};
template <>
structis_container<Container> : std::true_type {};
template <>
structis_container<DC1> : std::true_type {};
template <>
structis_container<DC2> : std::true_type {};
}
// or
namespacekk2
{
template <typename T>
structis_container : std::is_base_of<Container, T> {};
}
namespacedetail
{
template <typename T>
boolisCont(T const &in, std::true_type)
{
std::cout << "is container\n";
returntrue;
}
template <typename T>
boolisCont(T const &in, std::false_type)
{
std::cout << "is not a container\n";
returnfalse;
}
}
template <typename T>
boolisCont(T const &in)
{
returndetail::isCont(in, kk2::is_container<T>{});
}
intmain()
{
isCont(Container{});
isCont(DC1{});
isCont(DC2{});
isCont(Other{});
return0;
}