In a C++ template with the generic type T, I can use
const T &
to get a reference to a constant T. However, if now T itself is a reference type (like e.g. T = int &), the above term resolves to
int &
and not to
const int &
which quite makes sense, since any reference itself is always constant. However, is there still a way to require a
const T &
if T itself is a reference type?
Edit: sample code to evaluate (g++ compiler):
template <typename T> class TemplateClass
{
public:
void foo(const T &bar) { }
};
int main()
{
TemplateClass<int &> x;
x.foo(0); // <-- compile error: no conversion from int to int&
return 0;
}
Remove the reference:
Now it works as expected.