-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP416.SFINAEOutFunctionOverloads.cpp
More file actions
56 lines (49 loc) · 1.34 KB
/
Copy pathP416.SFINAEOutFunctionOverloads.cpp
File metadata and controls
56 lines (49 loc) · 1.34 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
#include <iostream>
#include <type_traits>
template<typename T>
struct IsDefaultConstructible
{
private:
// test for types who has a default constructor
template<typename U, typename = decltype(U())>
static char test(void*);
// test fallback
template<typename>
static long test(...);
public:
static constexpr bool value = std::is_same_v<decltype(test<T>(nullptr)), char>;
};
template<typename T>
constexpr bool IsDefaultConstructible_v = IsDefaultConstructible<T>::value;
// implementation 2
template<typename T>
struct IsDefaultConstructible2Helper
{
private:
// test for types who has a default constructor
template<typename U, typename = decltype(U())>
static std::true_type test(void*);
// test fallback
template<typename>
static std::false_type test(...);
public:
using type = decltype(test<T>(nullptr));
};
template<typename T>
struct IsDefaultConstructible2 : public IsDefaultConstructible2Helper<T>::type
{};
template<typename T>
constexpr bool IsDefaultConstructible2_v = IsDefaultConstructible2<T>::value;
struct X {};
struct Y
{
Y(int) {};
};
int main(int argc, char const *argv[])
{
static_assert(IsDefaultConstructible_v<X>);
static_assert(!IsDefaultConstructible_v<Y>);
static_assert(IsDefaultConstructible2_v<X>);
static_assert(!IsDefaultConstructible2_v<Y>);
return 0;
}