0
有人可以帮助我理解为什么下面的代码型扣,而使用通用引用
template< typename T >
void check()
{
std::cout << "unknow type" << std::endl;
}
template<>
void check<int>()
{
std::cout << "int" << std::endl;
}
template<>
void check<int&>()
{
std::cout << "int&" << std::endl;
}
template<>
void check<int&&>()
{
std::cout << "int&&" << std::endl;
}
template< typename T >
void bar(T&& a)
{
check<T>();
}
int main()
{
bar(0);
int a = 0;
bar(a);
}
的输出
int
int&
,而不是
int&&
int&
从我的观点。看来,r值参考仍然是一个r值参考值和一个l值参考值alue引用,但是,似乎只有l值引用保留为l值引用,r值变为非引用值。 这背后的动机是什么?
您正在使用'check',而不是'检查';为什么您希望'&&'在您明确省略时出现? –
ildjarn
我预计'bar(0)'与'(0)'相同。当用'bar (0)'在我的代码中替换'bar(0)'时,输出实际上是'int && \ n int&'。 –
“酒吧”的类型为“无效(T &&)”,“酒吧”的类型为“无效(T && &&)”(在参考折叠之前)。 –
Oktalist