【发布时间】:2014-01-02 05:26:56
【问题描述】:
编译器一直抱怨我试图将左值绑定到右值引用,但我看不出如何。我是 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.
}
调用此方法:
template <typename Key, typename Value, typename HashFunction, typename Equals>
void FastHash<Key, Value, HashFunction, Equals>::Insert(Key&& key, Value&& value)
{
// ...
}
我不断收到如下错误:
cannot convert argument 1 from 'std::string' to 'std::string &&'
在 Insert() 调用上。 key 不是在运算符重载中定义为右值吗?为什么它被重新解释为左值?
【问题讨论】:
标签: c++ c++11 move-semantics rvalue-reference