Template Type Deduction
suggest changeTemplate Generic Syntax
template<typename T>
void f(ParamType param);
f(expr);
Case 1: ParamType
is a Reference or Pointer, but not a Universal or Forward Reference. In this case type deduction works this way. The compiler ignores the reference part if it exists in expr
. The compiler then pattern-matches expr
’s type against ParamType
to determing T
.
template<typename T>
void f(T& param); //param is a reference
int x = 27; // x is an int
const int cx = x; // cx is a const int
const int& rx = x; // rx is a reference to x as a const int
f(x); // T is int, param's type is int&
f(cx); // T is const int, param's type is const int&
f(rx); // T is const int, param's type is const int&
Case 2: ParamType
is a Universal Reference or Forward Reference. In this case type deduction is the same as in case 1 if the expr
is an rvalue. If expr
is an lvalue, both T
and ParamType
are deduced to be lvalue references.
template<typename T>
void f(T&& param); // param is a universal reference
int x = 27; // x is an int
const int cx = x; // cx is a const int
const int& rx = x; // rx is a reference to x as a const int
f(x); // x is lvalue, so T is int&, param's type is also int&
f(cx); // cx is lvalue, so T is const int&, param's type is also const int&
f(rx); // rx is lvalue, so T is const int&, param's type is also const int&
f(27); // 27 is rvalue, so T is int, param's type is therefore int&&
Case 3: ParamType
is Neither a Pointer nor a Reference. If expr
is a reference the reference part is ignored. If expr
is const that is ignored as well. If it is volatile that is also ignored when deducing T’s type.
template<typename T>
void f(T param); // param is now passed by value
int x = 27; // x is an int
const int cx = x; // cx is a const int
const int& rx = x; // rx is a reference to x as a const int
f(x); // T's and param's types are both int
f(cx); // T's and param's types are again both int
f(rx); // T's and param's types are still both int
Found a mistake? Have a question or improvement idea?
Let me know.
Template Type Deduction
Table Of Contents
2 Literals
9 Arrays
10 Flow control
12 Loops
15 keywords
17 auto keyword
18 Pointers
21 std::string
22 Enumeration
24 std::vector
25 std::array
26 std::pair
27 std::map
30 std::any
31 std::variant
36 std::iomanip
37 Iterators
38 Basic I/O
39 File I/O
40 Streams
44 References
45 Polymorphism
52 Unions
53 Templates
54 Namespaces
57 Lambdas
58 Threading
60 Preprocessor
61 SFINAE
63 RAII
64 Exceptions
68 Sorting
75 Pimpl idiom
76 Copy elision
79 Singleton
82 Type erasure
85 RTTI
88 Scopes
89 Atomic types
91 constexpr
99 Type traits
103 Attributes
105 Profiling
108 Recursion
109 Callable objects
112 Inline functions
114 Header files
117 Parameter packs
118 Iteration
119 type deduction
121 Build systems
123 Type inference
126 Alignment
127 Inline variables
134 Optimization
135 Semaphore
137 Debugging
140 Mutexes
141 Recursive mutex
142 Unit testing
143 decltype
144 Digit separators
145 C++ Containers
147 Contributors