相关疑难解决方法(0)

Lvalue到rvalue引用绑定

编译器一直在抱怨我试图将左值绑定到右值引用,但我看不出如何.我是C++ 11的新手,移动语义等,所以请耐心等待.

我有这个功能:

template <typename Key, typename Value, typename HashFunction, typename Equals>
Value& FastHash<Key, Value, HashFunction, Equals>::operator[](Key&& key)
{
    //  Some code here...

    Insert(key, Value()); // Compiler error here

    //   More code here.
}
Run Code Online (Sandbox Code Playgroud)

它调用这个方法:

template <typename Key, typename Value, typename HashFunction, typename Equals>
void FastHash<Key, Value, HashFunction, Equals>::Insert(Key&& key, Value&& value)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我不断收到如下错误:

cannot convert argument 1 from 'std::string' to 'std::string &&'
Run Code Online (Sandbox Code Playgroud)

在Insert()调用上.是不是key在运算符重载中定义为右值?为什么它被重新解释为左值?

谢谢.

c++ rvalue-reference move-semantics c++11

17
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

c++11 ×1

move-semantics ×1

rvalue-reference ×1