c++模板实参推导

模板实参推导 - cppreference.com

引用折叠

  • T & & T & && T && &都被折叠为T &
  • T && &&折叠为T &&

模板实参推导表

int & const int & int && const int &&
T / const T (int) [T = int] (int) [T = int] (int) [T = int] (int) [T = int]
T & (int&) [T = int] (int const&) [T = int const] ERROR (int const&) [T = int const]
const T & (int const&) [T = int] (int const&) [T = int] (int const&) [T = int] (int const&) [T = int]
T && (int&) [T = int&] (int const&) [T = int const&] (int&&) [T = int] (int const&&) [T = int const]
const T && ERROR ERROR (int const&&) [T = int] (int const&&) [T = int]
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

using namespace std;
class {};
template<typename T>
void fooT(T t) {
cout << __PRETTY_FUNCTION__ << endl;
}
template<typename T>
void foocT(const T t) {
cout << __PRETTY_FUNCTION__ << endl;
}
template<typename T>
void fooTR(T &t) {
cout << __PRETTY_FUNCTION__ << endl;
}
template<typename T>
void foocTR(const T &t) {
cout << __PRETTY_FUNCTION__ << endl;
}
template<typename T>
void fooTr(T &&t) {
cout << __PRETTY_FUNCTION__ << endl;
}
template<typename T>
void foocTr(const T &&t) {
cout << __PRETTY_FUNCTION__ << endl;
}
void func() {
int i = 0;
const int ci = 0;
A2 a;
const A2 ca = A2();
cout << "int:" <<endl;
fooT(i);
foocT(i);
fooTR(i);
foocTR(i);
fooTr(i);

//foocTr(i);
cout << "void foocTr(const T &&) ERROR" <<endl;
cout << "const int:" <<endl;
fooT(ci);
foocT(ci);
fooTR(ci);
foocTR(ci);
fooTr(ci);

//foocTr(ci);
cout << "void foocTr(const T &&) ERROR" <<endl;
cout << "int &&:" <<endl;
fooT(move(i));
foocT(move(i));

//fooTR(move(i));
cout << "void fooTR(T &) ERROR" <<endl;
foocTR(move(i));
fooTr(move(i));
foocTr(move(i));
cout << "const int &&:" <<endl;
fooT(move(ci));
foocT(move(ci));
fooTR(move(ci));
foocTR(move(ci));
fooTr(move(ci));
foocTr(move(ci));
cout << "A:" << endl;
fooT(a);
foocT(a);
fooTR(a);
foocTR(a);
fooTr(a);

//foocTr(a);
cout << "void fooTr(const T &&) ERROR" << endl;
cout << "const A:" << endl;
fooT(ca);
foocT(ca);
fooTR(ca);
foocTR(ca);
fooTr(ca);

//foocTr(ca);
cout << "void fooTr(const T &&) ERROR" << endl;
cout << "A &&:" << endl;
fooT(move(a));
foocT(move(a));

//fooTR(move(a));
cout << "void fooTR(T &) ERROR" << endl;
foocTR(move(a));
fooTr(move(a));
foocTr(move(a));

cout << "const A &&:" << endl;
fooT(move(ca));
foocT(move(ca));
fooTR(move(ca));
foocTR(move(ca));
fooTr(move(ca));
foocTr(move(ca));
}
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
int:
void fooT(T) [T = int]
void foocT(const T) [T = int]
void fooTR(T &) [T = int]
void foocTR(const T &) [T = int]
void fooTr(T &&) [T = int &]
void foocTr(const T &&) ERROR
const int:
void fooT(T) [T = int]
void foocT(const T) [T = int]
void fooTR(T &) [T = const int]
void foocTR(const T &) [T = int]
void fooTr(T &&) [T = const int &]
void foocTr(const T &&) ERROR
int &&:
void fooT(T) [T = int]
void foocT(const T) [T = int]
void fooTR(T &) ERROR
void foocTR(const T &) [T = int]
void fooTr(T &&) [T = int]
void foocTr(const T &&) [T = int]
const int &&:
void fooT(T) [T = int]
void foocT(const T) [T = int]
void fooTR(T &) [T = const int]
void foocTR(const T &) [T = int]
void fooTr(T &&) [T = const int]
void foocTr(const T &&) [T = int]
A:
void fooT(T) [T = A2]
void foocT(const T) [T = A2]
void fooTR(T &) [T = A2]
void foocTR(const T &) [T = A2]
void fooTr(T &&) [T = A2 &]
void fooTr(const T &&) ERROR
const A:
void fooT(T) [T = A2]
void foocT(const T) [T = A2]
void fooTR(T &) [T = const A2]
void foocTR(const T &) [T = A2]
void fooTr(T &&) [T = const A2 &]
void fooTr(const T &&) ERROR
A &&:
void fooT(T) [T = A2]
void foocT(const T) [T = A2]
void fooTR(T &) ERROR
void foocTR(const T &) [T = A2]
void fooTr(T &&) [T = A2]
void foocTr(const T &&) [T = A2]
const A &&:
void fooT(T) [T = A2]
void foocT(const T) [T = A2]
void fooTR(T &) [T = const A2]
void foocTR(const T &) [T = A2]
void fooTr(T &&) [T = const A2]
void foocTr(const T &&) [T = A2]