-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassTemplateArgDeduction_CTAD.cpp
102 lines (85 loc) · 2.28 KB
/
ClassTemplateArgDeduction_CTAD.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* \file ClassTemplateArgDeduction_CTAD.cpp
* \brief CTAD - Class template argument deduction
*
* https://en.cppreference.com/w/cpp/language/class_template_argument_deduction
*
* In order to instantiate a class template, every template argument must be known,
* but not every template argument has to be specified.
* In the following contexts the compiler will deduce the template arguments from the type of the
* initializer
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
// new-expressions
template<class T>
class A
{
public:
explicit A(
const T a_t1,
const T a_t2
) :
_t1{a_t1},
_t2{a_t2}
{
}
void
print(std::ostream &out_os) const
{
out_os << "{" << _t1 << "," << _t2 << "}";
}
private:
const T _t1 {};
const T _t2 {};
};
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
// any declaration that specifies initialization of a variable and variable template
{
// deduces to std::pair<int, double> p(2, 4.5);
std::pair p(2, 4.5);
std::cout << STD_TRACE_VAR(p) << std::endl;
// same as auto t = std::make_tuple(4, 3, 2.5);
std::tuple t(4, 3, 2.5);
std::cout << STD_TRACE_VAR(t) << std::endl;
// same as std::less<void> l;
std::less l;
std::cout << STD_TRACE_VAR(l(1, 2)) << std::endl;
// std::vector<unsigned int>
std::vector v{1U, 2U, 3U};
std::cout << STD_TRACE_VAR(v) << std::endl;
}
// new-expressions
{
auto a = new A{'a', 'b'}; // allocated type is A<char>
std::cout << STD_TRACE_VAR(*a) << std::endl;
}
// function-style cast expressions
{
// deduces to std::lock_guard<std::mutex>
{
std::mutex m;
auto locker = std::lock_guard(m);
}
// deduces to std::back_insert_iterator<T>,
// where T is the type of the container vi2
if constexpr (0) {
std::vector<int> v1;
std::vector<int> v2;
std::copy_n(v1, 3, std::back_insert_iterator(v2));
}
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
p: std::pair: {2,4.5}
t: std::tuple (size=3): {4,3,2.5}
l(1, 2): 1
v: std::vector (size=3): {1,2,3}
*a: {a,b}
#endif