天道酬勤,学无止境

rvalue

Reference initialization in C++

Can anybody explain to me why there is a difference between these two statements? class A{}; const A& a = A(); // correct A& b = A(); // wrong It says invalid initialization of non-const reference of type A& from a temporary of type A Why does const matter here?

2021-06-14 02:25:24    分类:问答    c++   reference   lvalue   rvalue   const-reference

Why is overloading on just one ref-qualifier not allowed?

Apparently, overloading on ref-qualifiers is not allowed – this code won't compile if you remove either & or && (just the tokens, not their functions): #include <iostream> struct S { void f() & { std::cout << "Lvalue" << std::endl; } void f() && { std::cout << "Rvalue" << std::endl; } }; int main() { S s; s.f(); // prints "Lvalue" S().f(); // prints "Rvalue" } In other words, if you have two functions of the same name and type, you have to define both if you define either. I assume this is deliberate, but what's the reason? Why not allow, say, calling the && version for rvalues if it's defined

2021-06-13 09:22:52    分类:问答    c++   c++11   overloading   language-lawyer   rvalue

Why does as_const forbid rvalue arguments?

I wanted to ask why as_const forbids rvalue arguments, according to cppreference.com (i.e. why the Standards folks made it so, not why cppreference.com specifically quoted them on that. And also not where in the spec the intent of the committee is codified, just for making sure :))). This (artificial) example would yield an error (user wants to make it const to keep COW quiet) QChar c = as_const(getQString())[0]; Another question's answer notes that if we just remove the deletion of the rvalue reference overload, it would silently transform rvalues to lvalues. Right, but why not handle rvalues

2021-06-13 06:32:16    分类:问答    c++   rvalue   c++17

`const &&` 是否绑定到所有纯右值(和 xvalues)?(Does `const &&` bind to all prvalues (and xvalues)?)

问题 C++标准定义了以下函数删除; template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete; 这是为了通过禁止函数绑定到临时值(右值)来帮助确保函数不被滥用。 const &&是否绑定到所有右值,特别是纯右值? const &&绑定到所有“移动的对象”(xvalues;基本上是从std::move或类似返回的东西)? 我可以推理它应该,但我没有任何“证据”。 或者相反,是否存在右值(纯右值或 xvalue)不会绑定到const && ? 如果是这样,怎么会这样? 注意:从评论中可以看出一些清晰度,这个问题严重影响了经典右值,即纯右值类别。 回答1 T const&&可以绑定到类型T或const T右值。 来自 8.5.3 [dcl.init.ref]第 5 段: 5 - 对“cv1 T1”类型的引用由“cv2 T2”类型的表达式初始化,如下所示:[...] — 否则,[...] 引用应为右值引用。 [...] — 如果初始化表达式 — 是 xvalue、类纯右值、数组纯右值或函数左值,并且“cv1 T1”与“cv2 T2”引用兼容 [...] 然后引用绑定到初始化表达式的值 [...] 如果初始化表达式是非类类型的纯右值

2021-06-12 07:47:16    分类:技术分享    c++   c++11   language-lawyer   rvalue-reference   rvalue

Return rvalue reference vs return by value in function return type [duplicate]

This question already has answers here: c++11 Return value optimization or move? [duplicate] (4 answers) Closed 6 years ago. In my code I have a function that constructs a string from a piece of data and then returns it. This string isn't used anywhere else, so it's safe for the receiving side to use move-assignment or move-initialization on it. std::string ReadString(...) { ... return std::string(...) } This is basically what I have. Is there any point in making the function return type std::string&&, since the value being returned is an rvalue? std::string&& ReadString(...) { ... return std

2021-06-11 08:47:42    分类:问答    c++   rvalue-reference   rvalue   pass-by-rvalue-reference

C++:函数左值或右值(c++: function lvalue or rvalue)

问题 我刚刚通过阅读本页开始学习 c++11 中的右值引用,但我陷入了第一页。 这是我从该页面获取的代码。 int& foo(); foo() = 42; // ok, foo() is an lvalue int* p1 = &foo(); // ok, foo() is an lvalue int foobar(); j = foobar(); // ok, foobar() is an rvalue int* p2 = &foobar(); // error, cannot take the address of an rvalue 为什么foo()是左值? 是不是因为foo()返回int& ,它基本上是一个左值? 为什么foobar()是右值? 是因为foobar()返回int吗? 一般来说,你为什么要关心一个函数是否是右值? 我想如果我阅读那篇文章的其余部分,我会得到我的答案。 回答1 L 值是位置,R 值是实际值。 所以: 由于foo()返回一个引用( int& ),因此它本身就是一个左值。 正确的。 foobar()是一个右值,因为foobar()返回int 。 我们不太关心函数是否是 R 值。 我们感到兴奋的是 R 值引用。 你指的那篇文章很有趣,我之前没有考虑转发或在工厂使用。 我对 R-Value 引用感到兴奋的原因是移动语义,例如: BigClass my

2021-06-09 10:00:53    分类:技术分享    c++   c++11   lvalue   rvalue

Arrays and Rvalues (as parameters)

I wonder if there is any way to differentiate the function calls (with arrays as parameters) shown in the following code: #include <cstring> #include <iostream> template <size_t Size> void foo_array( const char (&data)[Size] ) { std::cout << "named\n"; } template <size_t Size> void foo_array( char (&&data)[Size] ) //rvalue of arrays? { std::cout << "temporary\n"; } struct A {}; void foo( const A& a ) { std::cout << "named\n"; } void foo( A&& a ) { std::cout << "temporary\n"; } int main( /* int argc, char* argv[] */ ) { A a; const A a2; foo(a); foo(A()); //Temporary -> OK! foo(a2); //----------

2021-06-05 16:14:15    分类:问答    c++   arrays   c++11   rvalue-reference   rvalue

Why does std::reference_wrapper<const T> not accept a temporary?

Normally, rvalues can bind to const references (const SomeType&). It's built into the language. However, std::reference_wrapper<const T> does not accept an rvalue as its constructor argument since the corresponding overload is deliberately deleted. What is the reason for this inconsistency? std::reference_wrapper is "advertised" as the alternative to a reference variable for cases when we must pass by value but would like to preserve reference semantics. In other words, if the rvalue to const & binding is considered safe, since it's built into the language, why did the designers of C++11 not

2021-06-04 12:15:22    分类:问答    c++   c++11   constants   rvalue   reference-wrapper

Go中临时地址?(Address of a temporary in Go?)

问题 处理这种情况的最干净的方法是什么: func a() string { /* doesn't matter */ } b *string = &a() 这会产生错误: 不能取 a() 的地址 我的理解是 Go 会自动将局部变量提升到堆中,如果它的地址被占用。 这里很明显是要取返回值的地址。 处理这个问题的惯用方法是什么? 回答1 地址操作符返回一个指向有“家”的东西的指针,例如一个变量。 代码中表达式的值是“无家可归”。 如果你真的需要一个 *string,你必须分两步完成: tmp := a(); b := &tmp 请注意,虽然 *string 有完全有效的用例,但很多时候使用它们是错误的。 在 Go 中, string是一种值类型,但它是一种廉价的传递方式(一个指针和一个 int)。 String 的值是不可变的,更改*string更改“home”指向的位置,而不是字符串值,因此在大多数情况下根本不需要*string 。 回答2 请参阅 Go 语言规范的相关部分。 &只能用于: 可寻址的东西:变量、指针间接、切片索引操作、可寻址结构的字段选择器、可寻址数组的数组索引操作; 或者复合字面量 你所拥有的两者都不是,所以它不起作用。 我什至不确定即使你能做到这意味着什么。 获取函数调用结果的地址? 通常,您将某个事物的指针传递给某人,因为您希望他们能够分配给所指向的事物

2021-06-02 08:58:33    分类:技术分享    pointers   return   go   temporary   rvalue

Pass lvalue to rvalue

I made a small 'blocking queue' class. It irritates me that I have created redundant code for values passed into the enqueue member function. Here are the two functions that do the same exact thing (except the rvalue uses std::move to move the rvalue into the actual queue collection), except handles lvalue and rvalue respectively: void enqueue(const T& item) { std::unique_lock<std::mutex> lock(m); this->push(item); this->data_available = true; cv.notify_one(); } void enqueue(T&& item) { std::unique_lock<std::mutex> lock(m); this->push(std::move(item)); this->data_available = true; cv.notify

2021-06-02 04:19:08    分类:问答    c++   c++11   lvalue   rvalue