相关疑难解决方法(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++ 11类型的变量?

在C++ 11中,我对类型之间的差异感到困惑T,reference to T因为它们适用于命名变量的表达式.具体考虑:

int main()
{
    int x = 42;
    int& y = x;

    x; // (1)
    y; // (2)
}
Run Code Online (Sandbox Code Playgroud)

x上面(1)中表达式的类型是什么?难道int还是lvalue reference to int?(它的值类别显然是一个lvalue,但这与它的类型是分开的)

同样y,上面(2)中表达式的类型是什么?难道int还是lvalue reference to int

它在5.1.1.8中说:

[标识符主表达式]的类型是标识符的类型.结果是由标识符表示的实体.如果实体是函数,变量或数据成员,则结果是左值,否则为prvalue.

c++ c++11

3
推荐指数
1
解决办法
307
查看次数

标签 统计

c++ ×2

c++11 ×2

move-semantics ×1

rvalue-reference ×1