rvalue

What kind of syntactic sugar is available in Perl to reduce code for l/rvalue operators vs. if statements?

北城以北 提交于 2019-12-04 16:32:49
问题 There's a bunch out there, as Perl is a pretty sugary language, but the most used statements in any language is the combination of if statements and setting values. I think I've found many of them, but there's still a few gaps. Ultimately, the goal would be to not have to write a variable name more than once: Here's what I have so far: $r ||= $s; # $r = $s unless ($r); $r //= $s; # $r = $s unless (defined $r); $r &&= $s; # $r = $s if ($r); $r = $c ? $s : $t; # if ($c) { $r = $s } else { $r =

c++: function lvalue or rvalue

 ̄綄美尐妖づ 提交于 2019-12-04 08:39:29
I just started learning about rvalue references in c++11 by reading this page , but I got stuck into the very first page. Here is the code I took from that page. 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 why is foo() an lvalue? is it because foo() returns int& which is basically an lvalue? why is foobar() an rvalue? is it because foobar() returns int ? In general, why would you care if a function is an rvalue or not? I

What makes moving objects faster than copying?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 07:42:45
问题 I have heard Scott Meyers say " std::move() doesn't move anything" ... but I haven't understood what it means. So to specify my question consider the following: class Box { /* things... */ }; Box box1 = some_value; Box box2 = box1; // value of box1 is copied to box2 ... ok What about: Box box3 = std::move(box1); I do understand the rules of lvalue and rvalue but what I don't understand is what is actually happening in the memory? Is it just copying the value in some different way, sharing an

swap temporary tuples of references

醉酒当歌 提交于 2019-12-04 06:56:23
I'm writing a custom iterator that, when dereferenced returns a tuple of references. Since the tuple itself is ephemeral, I don't think I can return a reference from operator*(). I think my iterator makes sense semantically, since it has reference semantics, even though operator* returns a value. The issue is, when I try to call std::swap (or rather, when std::sort does), like below, I get errors because the swap expects l-values. Is there an easy fix to this problem? #include <vector> class test { public: test() :v1(10), v2(10) {} class iterator { public: iterator(std::vector<int>& _v1, std:

Arrays and Rvalues (as parameters)

為{幸葍}努か 提交于 2019-12-04 04:47:59
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); //----------

where is rvalue stored in c?

你说的曾经没有我的故事 提交于 2019-12-03 19:59:48
问题 in C, i have this code piece: int a; a = 10 + 5 - 3 I want to ask: where is (10+5-3) stored at? (As far as I know, a is located on stack, how about (10+5-3) ? How does this rvalue get calculated?) 回答1: Typically, the r-value is "stored" within the program itself. In other words, the compiler itself ( before the program is ever run ) computes the 10 + 5 - 3 value (it can do so since since it is all based on constant immediate values), and it emits the assembly code to store the result of this

C++11: Why is assigning rvalues allowed?

不打扰是莪最后的温柔 提交于 2019-12-03 19:38:10
问题 From what I understand the reason why it is dangerous to return rvalues references to from functions is due to the following code: T&& f(T&& x) { do_something_to_T(x); return static_cast<T&&>(x); } T f(const T& x) { T x2 = x; do_something_to_T(x2); return x2; } T&& y = f(T()); This leaves y as an undefined dangling reference. However, I don't understand why the above code even compiles? Is there ever a legitimate reason to assign a rvalue reference to another rvalue reference? Aren't rvalues

What kind of syntactic sugar is available in Perl to reduce code for l/rvalue operators vs. if statements?

拥有回忆 提交于 2019-12-03 10:28:49
There's a bunch out there, as Perl is a pretty sugary language, but the most used statements in any language is the combination of if statements and setting values. I think I've found many of them, but there's still a few gaps. Ultimately, the goal would be to not have to write a variable name more than once: Here's what I have so far: $r ||= $s; # $r = $s unless ($r); $r //= $s; # $r = $s unless (defined $r); $r &&= $s; # $r = $s if ($r); $r = $c ? $s : $t; # if ($c) { $r = $s } else { $r = $t } $c ? $r : $s = $t; # if ($c) { $r = $t } else { $s = $t } $r = $s || $t; # if ($s) { $r = $s }

Exact difference between rvalue and lvalue

浪尽此生 提交于 2019-12-03 03:23:35
While I was reading http://thbecker.net/articles/rvalue_references/section_01.html , I got following snippiest. // lvalues: // int i = 42; i = 43; // ok, i is an lvalue int& foo(); foo() = 42; // ok, foo() is an lvalue int* p1 = &foo(); // ok, foo() is an lvalue // rvalues: // int foobar(); int j = 0; j = foobar(); // ok, foobar() is an rvalue int* p2 = &foobar(); // error, cannot take the address of an rvalue j = 42; // ok, 42 is an rvalue Why int* p2 = &foobar(); is error statement, while int* p1 = &foo(); is not an error. How later one is lvalue while first one is rvalue? Thanks in advance

Best form for constructors? Pass by value or reference?

荒凉一梦 提交于 2019-12-03 02:31:25
问题 I'm wondering the best form for my constructors. Here is some sample code: class Y { ... } class X { public: X(const Y& y) : m_y(y) {} // (a) X(Y y) : m_y(y) {} // (b) X(Y&& y) : m_y(std::forward<Y>(y)) {} // (c) Y m_y; } Y f() { return ... } int main() { Y y = f(); X x1(y); // (1) X x2(f()); // (2) } From what I understand, this is the best the compiler can do in each situation. (1a) y is copied into x1.m_y (1 copy) (1b) y is copied into the argument of the constructor of X, and then copied