以 rvaule 引用作为参数的函数模板重载不起作用?
function template overload with rvaule reference as argument does not work?
当输入不是右值时,以&& 为参数的函数模板似乎无法重载。以 here 为例:
template<typename A, typename B>
void test_tp_func(A&& a, B&& b)
{
std::cout<<"tp1(" << a << "," << b << ")\n";
}
template<typename A>
void test_tp_func(A&& a, int&& b)
{
std::cout<<"tp2(" << a << "," << b << ")\n";
}
int main()
{
test_tp_func(1, 2);
int i{10};
const int& ir = i;
test_tp_func(2, ir);
test_tp_func(2, std::move(i));
}
输出为:
tp2(1,2)
tp1(2,10)
tp2(2,10)
我们可以看到 test_tp_func(2, ir);
根本没有使用重载。我怎样才能确保它使用 test_tp_func(A&& a, int&& b)
?一种方法是在 B 为 int 时添加 enable_if_t 以禁用原始模板。但是,原始模板 test_tp_func(A&& a, B&& b)
定义在一个不受我控制的文件中。
更新示例以实际使用 const&
当输入不是右值时,以&& 为参数的函数模板似乎无法重载。以 here 为例:
template<typename A, typename B>
void test_tp_func(A&& a, B&& b)
{
std::cout<<"tp1(" << a << "," << b << ")\n";
}
template<typename A>
void test_tp_func(A&& a, int&& b)
{
std::cout<<"tp2(" << a << "," << b << ")\n";
}
int main()
{
test_tp_func(1, 2);
int i{10};
const int& ir = i;
test_tp_func(2, ir);
test_tp_func(2, std::move(i));
}
输出为:
tp2(1,2)
tp1(2,10)
tp2(2,10)
我们可以看到 test_tp_func(2, ir);
根本没有使用重载。我怎样才能确保它使用 test_tp_func(A&& a, int&& b)
?一种方法是在 B 为 int 时添加 enable_if_t 以禁用原始模板。但是,原始模板 test_tp_func(A&& a, B&& b)
定义在一个不受我控制的文件中。
更新示例以实际使用 const&