Consider something like:
template <typename T>
void f(T& x)
{
....
}
Why is something kind of attached to ? const intf(T&)
This seems like a kind of const-correctness violation. In fact, if it f()accepts a non-const reference T&, then it is very likely that it f()will change its argument (otherwise f()it would be defined as void f(const T&)).
In this code:
template <typename T>
inline void f(T& x)
{
x = 0;
}
int main()
{
int n = 2;
f(n);
const int cn = 10;
f(cn);
}
the compiler tries to call f()with T = const int, then, of course, an error message appears due to assignment x = 0;inside the body f().
This is the error message from GCC:
test.cpp: In instantiation of 'void f(T&) [with T = const int]':
test.cpp:13:9: required from here
test.cpp:4:7: error: assignment of read-only reference 'x'
x = 0;
^
But why is the compiler trying to bind the const argument to a function template that takes a non-constant parameter?
++?