-
Notifications
You must be signed in to change notification settings - Fork 2
/
week14-app6.cpp
53 lines (40 loc) · 1.54 KB
/
week14-app6.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
// SFINAE: enable_if, and its relation with concepts
// ADL: argument dependent lookup, friend functions
// CRTP: curiously recurring template pattern
#include <iostream>
using namespace std;
template<bool B, typename T = void> struct enableIf { };
template<typename T> struct enableIf<true, T> { using type = T; };
template<typename T>
concept IsIntegral = is_integral_v<T>;
//template<typename Integral, typename T = enableIf< is_integral_v<Integral> >::type >
//void printIntegralValues(Integral integral)
//{
// cout << integral << endl;
//}
//template<typename Integral>
//enableIf< is_integral_v<Integral> >::type printIntegralValues(Integral integral)
//{
// cout << integral << endl;
//}
template<typename Integral>
void printIntegralValues(Integral integral,
typename enableIf<is_integral_v<Integral>>::type* /*whatever*/ = nullptr)
// when Integral typename is really integral:
// enableIf line turns into "void* whatever = nullptr" which is correct syntax
// else ::type is not available, hence, skipped by the substitution failure is not an error rule of compiler
{
cout << integral << endl;
}
void foo(int* whatever = nullptr) // you can ignore writing whatever variable name if you don't use it
{
}
int main()
{
// SFINAE -> Substitution Failure Is Not An Error
// invented trick to earn the concept like behavior with old compilers
// have concepts before they are introduced in C++20
// printIntegralValues("Hi there"); // invalid
printIntegralValues(3); // valid
foo();
}