I wrote this code:
#include <iostream>
void test(std::string && x) {
std::cout << "test: 1" << std::endl;
}
void test(const std::string & x) {
std::cout << "test: 2" << std::endl;
}
template<typename T>
void test2(T&& x) {
std::cout << "test2: 1" << std::endl;
}
template<typename T>
void test2(const T & x) {
std::cout << "test2: 2" << std::endl;
}
int main()
{
std::string x = "aaa";
std::string & y = x;
test(y);
test2(y);
return 0;
}
It has 4 functions, two named test
and two test2
. test2
have argument choosen by template and test
has it static. Both types have one overload for const T & x
and one for T && x
. But if i call them with T &
, different type is called. Result of code above is:
test: 2
test2: 1
How, and more importantly why, is this happening?