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
;