I have started to learn C++ and currently I am trying to get started with templates, so please bear with me, if my wording is not 100% accurate.
I am using the following literature:
- C++ Templates: The Complete Guide (2nd Edition)
- Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14
The first book takes a look at the following template function
template<typename T1, typename T2>
auto max(T1 a, T2 b) -> decltype(b<a?a:b) {
return b < a ? a : b;
}
and states that this definition has a drawback, since T1
or T2
might be a reference, so that the return type could be a reference type.
However, the second book states, that, if the ParamType
, in our case T1
and T2
is neither a pointer nor a reference, which is true for our case, the reference part of the calling expression is ignored.
Illustrated with the example
template<typename T>
void f(T param);
int x = 27; // as before
const int cx = x; // as before
const int& rx = x; // as before
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
Now I am wondering, how could it even be possible, that the return type of the first code snippet is a reference type?