Type "test" is const char[5] . When a function template is called and an argument is used to output a parameter of a reference type, conversion between arrays and pointers does not occur. Thus, the general function outputs T as const char[5] - you can see that the output for "test" and for const char* is different:
template<typename T> void func(T& s){ std::cout << "in generic\n"; std::cout << typeid(s).name() << '\n'; } int main() { func("test"); const char *c = "test"; func(c); }
GCC Output:
in generic A5_c in generic PKc
On the other hand, in a nonequivalent function, you want to refer to a pointer that is not a constant, to const char . However, since the type "test" is const char[5] , it must undergo the transformation of the matrix into a pointer, which means that the resulting const char* is temporary (rvalue). Therefore, it cannot communicate with reference to non-constant.
source share