Tag: rvalue

这里不是const修饰符吗?

“ 有效的C ++ ”第3项说“尽可能使用常量”,它给出了一个例子: const Rational operator*(const Rational& lhs, const Rational& rhs); 防止客户犯下这样的暴行: Rational a, b, c; … (a * b) = c; // invoke operator= on the result of a*b! 但不是函数的非参考返回值已经是右值 ? 那么为什么要这样做呢?

在C ++中,哪些类(左值,右值,左值等)可以生成临时types的expression式?

以下是一些示例代码: #include <iostream> class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() { data += 1; return *this; } void *get_addr() { return (void*)this; } friend Foo operator + (const Foo& lhs, const Foo& rhs); friend std::ostream& operator << (std::ostream& os, const Foo& f); private: int data; }; std::ostream& operator << (std::ostream& […]

非等级rvalues总是有cv不合格的types

§3.10第9节规定:“非阶级的价值总是有cv不合格的types”。 这让我想知道 int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvalue\n"; } void pass_int(const int&& i) { std::cout << "const rvalue\n"; } int main() { pass_int(foo()); // prints "rvalue" pass_int(bar()); // prints "const rvalue" } 根据标准,对于非typestypes来说,不存在常量右值,但bar()更喜欢绑定到const int&& 。 这是一个编译器错误? 编辑:显然, this也是一个const rvalue 🙂 编辑:这个问题似乎是修复在g […]

右值参考被视为左值?

我发布了这个答案: https : //stackoverflow.com/a/28459180/2642059其中包含以下代码: void foo(string&& bar){ string* temp = &bar; cout << *temp << " @:" << temp << endl; } 是一个右值还是左值? 我问,因为我显然不能接受一个右值的地址,但我可以像在这里所做的那样,使用右值引用的地址。 如果你可以对右值引用进行右值引用的任何操作,那么使用“&&”而不是“&”区分两者的意义何在?