rvalue-reference

C++11 rvalues and move semantics confusion (return statement)

穿精又带淫゛_ 提交于 2019-11-25 23:06:22
问题 I\'m trying to understand rvalue references and move semantics of C++11. What is the difference between these examples, and which of them is going to do no vector copy? First example std::vector<int> return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return tmp; } std::vector<int> &&rval_ref = return_vector(); Second example std::vector<int>&& return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return std::move(tmp); } std::vector<int> &&rval_ref = return_vector(); Third example std:

What does T&& (double ampersand) mean in C++11?

折月煮酒 提交于 2019-11-25 23:05:26
问题 I\'ve been looking into some of the new features of C++11 and one I\'ve noticed is the double ampersand in declaring variables, like T&& var . For a start, what is this beast called? I wish Google would allow us to search for punctuation like this. What exactly does it mean? At first glance, it appears to be a double reference (like the C-style double pointers T** var ), but I\'m having a hard time thinking of a use case for that. 回答1: It declares an rvalue reference (standards proposal doc).

Advantages of using forward

混江龙づ霸主 提交于 2019-11-25 22:56:06
问题 In perfect forwarding, std::forward is used to convert the named rvalue references t1 and t2 to unnamed rvalue references. What is the purpose of doing that? How would that affect the called function inner if we leave t1 & t2 as lvalues? template <typename T1, typename T2> void outer(T1&& t1, T2&& t2) { inner(std::forward<T1>(t1), std::forward<T2>(t2)); } 回答1: You have to understand the forwarding problem. You can read the entire problem in detail, but I'll summarize. Basically, given the

Rule-of-Three becomes Rule-of-Five with C++11?

守給你的承諾、 提交于 2019-11-25 22:13:58
问题 So, after watching this wonderful lecture on rvalue references, I thought that every class would benefit of such a \"move constructor\", template<class T> MyClass(T&& other) edit and of course a \"move assignment operator\", template<class T> MyClass& operator=(T&& other) as Philipp points out in his answer, if it has dynamically allocated members, or generally stores pointers. Just like you should have a copy-ctor, assignment operator and destructor if the points mentioned before apply.