下面是代码:
template<class T> <typename my_set<T>::const_iterator, bool>
my_set<T>::insert(const value_type &val)
{
// does all kinds of things
}
template<class T>
pair<typename my_set<T>::const_iterator, bool> my_set<T>::insert(value_type &&val)
{
return insert(std::move(val)); // should call the func above
};
但是当我运行它时,它会递归地调用自身+ CLion调试器说: 命名空间“ std”中没有符号“ move”。
一半。
答案 0 :(得分:0)
问题是std::move
再次使val
右值引用,并且您递归调用同一函数。如果要先调用第二个函数,则需要直接传递val
:
return insert(val);
尽管不清楚为什么需要,因为只需消除第二个函数就可以达到相同的目的(然后,rvalue引用将绑定到const lvalue引用,因为没有更好的匹配)
注意:这在您的代码中还不清楚,但是如果您想让第二个函数进行实际移动,您的方法是错误的,但这是一个常见错误。 std::move
本身不会移动任何东西,它只是将命名对象类型更改为右值引用,然后实际移动发生在类的移动构造函数或移动赋值运算符中(如果已实现) )。
简单的解决方案可能只是具有一个功能并按值传递对象或使用转发(也称为通用引用):
template<class T> std::pair<typename my_set<T>::const_iterator, bool>
my_set<T>::insert( value_type val)
{
...
internal_object = std::move( val );
...
}