I have the following function:
template<typename T>
void f(const T& val) {
using value_type = T;
using sub_type = typename value_type::sub_type;
//etc...
}
However, I am running into the problem that the compiler is telling me that T is not fact whatever type it is, but instead a reference to it. How is that possible, in what circumstances are template parameter for const-references themselves references?
Note, I can work around the above issue with:
using value_type = std::remove_reference_t<T>;
but I'd like to understand what circumstances T, itself, would be a reference.
These things are independent. We have a template parameter,
T. And then we a have a function parameter that happens to beT const&. But the two aren't tied together.While
Twould never be deduced to a reference type based on that function parameter, deduction isn't the only way to provide template parameters:Here, I am explicitly providing
int&as the template argument forT, it is not deduced.T, here, would beint&(notint) and indeedvalisn't even a reference toconsthere, it would actually be anint&(not aint const&).Of course the typical usage would be just
f(i)(no explicit template arguments) which would deduceTasintand havevalbe of typeint const&.