【发布时间】:2019-03-17 18:01:36
【问题描述】:
这是困扰我的代码
#include <iostream>
#include "DataItem.h"
void testRef( const int & param )
{
std::cout << "Lvalue reference" << std::endl;
}
void testRef( int && param )
{
std::cout << "Rvalue reference" << std::endl;
// Here's the thing I can't get. Why param is lvalue reference here??
testRef( param );
}
template<class T>
void func( T && param )
{
testRef( std::forward<T>( param ) );
}
int main2()
{
int a = 12;
func( a );
std::cout << "=================" << std::endl;
func( 14 );
std::cout << "=================" << std::endl;
return 0;
}
当我在testRef( int && param ) 中调用testRef() 时,我假设参数是右值引用,而不是右值函数将被调用(是的,永恒递归会发生)。但是调用了左值函数。为什么?
【问题讨论】:
-
一旦有了名字,它就是一个左值。
-
嗯!这是否意味着我应该在这种情况下使用
std::move? -
请从 cmets 移出您的答案。然后我可以将其标记为答案
标签: c++ rvalue type-deduction