Monero
Loading...
Searching...
No Matches
sfinae_helpers.h
Go to the documentation of this file.
1// Copyright (c) 2016-2022, The Monero Project
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#pragma once
30
31// the loose definitions of types in this file are, well, loose.
32//
33// these helpers aren't here for absolute type certainty at compile-time,
34// but rather to help with templated functions telling types apart.
35
36namespace sfinae
37{
38
39 typedef char true_type;
40
41 struct false_type { true_type a[2]; };
42
43 template <typename T>
45 {
46 private:
47
48 // does not have const iterator
49 template <typename C> static false_type c_iter(typename C::const_iterator*);
50 template <typename C> static true_type c_iter(...);
51
52 // does not have value_type
53 template <typename C> static false_type v_type(typename C::value_type*);
54 template <typename C> static true_type v_type(...);
55
56 // does not have key_type
57 template <typename C> static false_type k_type(typename C::key_type*);
58 template <typename C> static true_type k_type(...);
59
60 // does not have mapped_type
61 template <typename C> static false_type m_type(typename C::mapped_type*);
62 template <typename C> static true_type m_type(...);
63
64 public:
65
66 static const bool value = (
67 (
68 sizeof(c_iter<T>(0)) == sizeof(true_type) &&
69 sizeof(v_type<T>(0)) == sizeof(true_type) &&
70 sizeof(k_type<T>(0)) == sizeof(true_type) &&
71 sizeof(m_type<T>(0)) == sizeof(true_type)
72 )
73 || std::is_same<T, std::string>::value
74 );
75
76 typedef T type;
77 };
78
79 template <typename T>
81 {
82 private:
83
84 // has const iterator
85 template <typename C> static true_type c_iter(typename C::const_iterator*);
86 template <typename C> static false_type c_iter(...);
87
88 // has value_type
89 template <typename C> static true_type v_type(typename C::value_type*);
90 template <typename C> static false_type v_type(...);
91
92 // does not have key_type
93 template <typename C> static false_type k_type(typename C::key_type*);
94 template <typename C> static true_type k_type(...);
95
96 // does not have mapped_type
97 template <typename C> static false_type m_type(typename C::mapped_type*);
98 template <typename C> static true_type m_type(...);
99
100 public:
101
102 static const bool value = (
103 sizeof(c_iter<T>(0)) == sizeof(true_type) &&
104 sizeof(v_type<T>(0)) == sizeof(true_type) &&
105 sizeof(k_type<T>(0)) == sizeof(true_type) &&
106 sizeof(m_type<T>(0)) == sizeof(true_type) &&
107 !std::is_same<T, std::string>::value
108 );
109
110 typedef T type;
111 };
112
113 template <typename T>
115 {
116 private:
117
118 // has const iterator
119 template <typename C> static true_type c_iter(typename C::const_iterator*);
120 template <typename C> static false_type c_iter(...);
121
122 // has value_type
123 template <typename C> static true_type v_type(typename C::value_type*);
124 template <typename C> static false_type v_type(...);
125
126 // has key_type
127 template <typename C> static true_type k_type(typename C::key_type*);
128 template <typename C> static false_type k_type(...);
129
130 // has mapped_type
131 template <typename C> static true_type m_type(typename C::mapped_type*);
132 template <typename C> static false_type m_type(...);
133
134 public:
135
136 static const bool value = (
137 sizeof(c_iter<T>(0)) == sizeof(true_type) &&
138 sizeof(v_type<T>(0)) == sizeof(true_type) &&
139 sizeof(k_type<T>(0)) == sizeof(true_type) &&
140 sizeof(m_type<T>(0)) == sizeof(true_type) &&
141 !std::is_same<T, std::string>::value
142 );
143
144 typedef T type;
145 };
146
147} // namespace sfinae
Definition sfinae_helpers.h:37
char true_type
Definition sfinae_helpers.h:39
Definition sfinae_helpers.h:41
true_type a[2]
Definition sfinae_helpers.h:41
Definition sfinae_helpers.h:115
static const bool value
Definition sfinae_helpers.h:136
static false_type m_type(...)
static true_type k_type(typename C::key_type *)
static true_type v_type(typename C::value_type *)
static false_type v_type(...)
T type
Definition sfinae_helpers.h:144
static false_type c_iter(...)
static true_type m_type(typename C::mapped_type *)
static true_type c_iter(typename C::const_iterator *)
static false_type k_type(...)
Definition sfinae_helpers.h:45
static true_type m_type(...)
static false_type v_type(typename C::value_type *)
static true_type c_iter(...)
static true_type v_type(...)
static const bool value
Definition sfinae_helpers.h:66
static false_type k_type(typename C::key_type *)
static true_type k_type(...)
T type
Definition sfinae_helpers.h:76
static false_type c_iter(typename C::const_iterator *)
static false_type m_type(typename C::mapped_type *)
Definition sfinae_helpers.h:81
static false_type m_type(typename C::mapped_type *)
static true_type k_type(...)
static true_type c_iter(typename C::const_iterator *)
static false_type c_iter(...)
static const bool value
Definition sfinae_helpers.h:102
static true_type v_type(typename C::value_type *)
static false_type k_type(typename C::key_type *)
static true_type m_type(...)
T type
Definition sfinae_helpers.h:110
static false_type v_type(...)
#define T(x)