-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdiscrim-custom.cc
54 lines (42 loc) · 1.3 KB
/
discrim-custom.cc
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
//-----------------------------------------------------------------------------
//
// Source code for MIPT masters course on C++
// Slides: https://sourceforge.net/projects/cpp-lects-rus
// Licensed after GNU GPL v3
//
//-----------------------------------------------------------------------------
//
// custom Void_t based function discrimination: working void_t
//
//-----------------------------------------------------------------------------
#include "gtest/gtest.h"
#include <type_traits>
namespace {
template <typename T> struct TypeIdentity { using type = T; };
template <typename...> struct Void : TypeIdentity<void> {};
template <typename... Args> using Void_t = typename Void<Args...>::type;
// ensure that new Void_t is still alias to void
TEST(mysfinae, newvoid) {
auto res = std::is_same_v<std::void_t<int>, Void_t<int>>;
EXPECT_EQ(res, true);
}
// ----------------------------------------------------------------------------
struct One {
using fst = int;
};
struct Two {
using snd = int;
};
template <typename T, Void_t<typename T::fst> * = nullptr> int foo() {
return 1;
}
template <typename T, Void_t<typename T::snd> * = nullptr> int foo() {
return 2;
}
} // namespace
TEST(sfinae, discrimfixed) {
auto x = foo<One>();
auto y = foo<Two>();
EXPECT_EQ(x, 1);
EXPECT_EQ(y, 2);
}