rvalue-reference

C++11 constructor argument: std::move and value or std::forward and rvalue reference

家住魔仙堡 提交于 2019-12-03 21:46:24
Which of the below two should be preferred and why? struct X { Y data_; explicit X(Y&& data): data_(std::forward<Y>(data)) {} }; vs struct X { Y data_; explicit X(Y data): data_(std::move(data)) {} }; The two variants differ in functionality. The following statements work for the second one–but not for the first one: Y y; X x(y); If you are looking for the same functionality, the two variants should look as follows: struct X { Y data_; explicit X(const Y& data) : data_(data) { } explicit X(Y&& data) : data_(std::move(data)) { } }; struct X { Y data_; explicit X(Y data) : data_(std::move(data))

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

rvalue function overloading

我的未来我决定 提交于 2019-12-03 17:01:38
I want to overload a function so that it manipulates its argument in some way and then returns a reference to the argument – but if the argument is not mutable, then it should return a manipulated copy of the argument instead. After messing around with it for ages, here's what I've come up with. using namespace std; string& foo(string &in) { in.insert(0, "hello "); return in; } string foo(string &&in) { return move(foo(in)); } string foo(const string& in) { return foo(string(in)); } This code seem to work correctly, but I'm interested to hear if anyone can think of a better way to do it. Here

About catching exception good practices

余生颓废 提交于 2019-12-03 16:01:00
问题 I'm writing a little program in C++11 and really use exceptions for one of the first time. I've got a question about how to catch the exceptions efficiently, and after some googling I still don't have the answer. Here is the question : What is the more efficient (or recommended) between catching the exception by (const?) lvalue reference, or by (const?) rvalue reference? In code this give : 1) try { throw std::exception{"what"}; } catch (std::exception& ex) {} 2) try { throw std::exception{

Difference between returning a const reference and rvalue reference

三世轮回 提交于 2019-12-03 13:45:56
If I'm not wrong, I think that both a const reference and a rvalue reference can bind to a rvalue. Is there any practical difference between a function that returns the former and a function that returns the latter? EDIT. I cannot modify the former, but why would I be interested in modifying a rvalue? Does it make sense? A const lvalue reference can bind to anything. An rvalue reference can only bind to non- const rvalues. non-const lvalue const lvalue non-const rvalue const rvalue const T& yes yes yes yes T&& no no yes no As you can see, they are very different. In addition, if a function

Explicit ref-qualified conversion operator templates in action

天大地大妈咪最大 提交于 2019-12-03 13:09:24
Given the following conversion operators struct A { template<typename T> explicit operator T&& () &&; template<typename T> explicit operator T& () &; template<typename T> explicit operator const T& () const&; }; struct B {}; I would expect the following conversions to be all valid, yet some give compile errors ( live example ): A a; A&& ar = std::move(a); A& al = a; const A& ac = a; B&& bm(std::move(a)); // 1. OK B&& bt(A{}); // 2. OK B&& br(ar); // 3. error: no viable conversion from A to B B& bl(al); // 4. OK const B& bz(al); // 5. OK const B& bc(ac); // 6. OK B cm(std::move(a)); // 7. error

Do I use std::forward or std::move here?

做~自己de王妃 提交于 2019-12-03 12:25:39
问题 Let's say I have: template<class T> struct NodeBase { T value; NodeBase(T &&value) : value(value) { } }; and I inherit from it: template<class T> struct Node : public NodeBase<T> { Node(T &&value) : NodeBase( WHAT_GOES_HERE (value)) { } }; Should WHAT_GOES_HERE be std::move or std::forward<T> ? Why? 回答1: Since in the implementation of the constructor of Node<T> it is unknown whether T is a plain type (i.e. not a reference), or a reference, std::forward<T>(value) is suitable. std::forward<T>

Can I use rvalue reference to temporary? Is it undefined behavior or not?

一个人想着一个人 提交于 2019-12-03 11:19:01
Updating the question Why this two rvalue references examples have different behavior? : Source code: int a = 0; auto && b = a++; ++a; cout << a << b << endl; prints 20 Is it undefined behavior (UB) to use b after the a++ call? Maybe we cannot use b because it refers to a temporary? No it is not undefined behavior (UB). It's fine - you can modify the contents of the temporary here (so long as the reference is valid for the lifetime of the temporary, in this case the bind to the rvalue reference extends that lifetime of the rvalue to the lifetime of the reference). A more general question is;

C++11 lambda can be assigned to std::function with incorrect signature

一世执手 提交于 2019-12-03 10:58:17
The following compiles and runs (under Apple LLVM version 6.1.0 and Visual C++ 2015): #include <functional> #include <iostream> struct s { int x; }; int main(int argc, char **argv) { std::function<void (s &&)> f = [](const s &p) { std::cout << p.x; }; f(s {1}); return 0; } Why doesn't the assignment std::function<void (s &&)> f = [](const s &p) { std::cout << p.x; }; generate an error? A function accepting an rvalue reference should not have the same signature as a function accepting a const lvalue reference, should it? Dropping the const from the lambda's declaration does generate an error as

Why doesn't `std::stringstream::stringstream(std::string&&)` exist?

十年热恋 提交于 2019-12-03 10:27:08
问题 I was hoping stringstream has a constructor that steals its initial content from a string&& . Do such inter-species "move constructors" generally not exist in the STL? If not, why not? 回答1: There's history, which is disappointing. But also a future that looks bright. When move semantics went into C++11, it was huge, controversial, and overwhelming. I wanted to be able to move strings into and out of stringstream . However the politics at the time demanded that the internal store did not have