It is a bit confusing to me about how C++ 11 does template deduction when const references to a template parameter types are used. Consider the following program:
template <typename T> void test_func(const T &a){
(*a)++;
}
int main() {
// 1st case
int i = 1;
test_func(&i);
// 2nd case
const int* cPtr = &i;
test_func(cPtr);
}
My questions are:
- For the first case, it is compiled fine; so it seems like the instantiated template function parameter is
int* const &a
(a top-level const); but if we directly replaceT with
int*, we get
const int* &a` (a low-level const) and compiler should have failed; how can we express a low-level const in a format of "const T &a"; I am confused what is the real type of the parameter and what is the type of T; - For the second case, compilation fails with an
It seems like the second instantiated function inherits the low-level const as well; then what is the type of parameter; what is the type oferror: increment of read-only location `*(const int*)a;'
T
;
Your
test_func()
receives a const reference to something. In the first case, the something is the address of an integer, so the final type is "a reference which cannot be used to change its referent which is a pointer to an integer." This means you can use the pointer to the integer, including using it to change the integer, but you cannot change the value of the pointer (i.e. the address) to point to something else.In the second case the final type is "a reference which cannot be used to change its referent which is a pointer to an integer and the pointer cannot be used to change the integer." Therefore,
*a
isconst int
which cannot be modified.