rvalue-reference

Do I need to overload methods accepting const lvalue reference for rvalue references explicitly?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:48:47
问题 currently I’m playing around with rvalue reference (C++11, g++ with gnu++x0) and I want to implement move semantics in my classes, because it just feels „right“. Do I need to overload each function which normally would accept const lvalue reference to benefit from the rvalue references? Let’s say this is my example class: class Person { public: Person() = default; Person(std::string &name); Person(const Person &rhs); Person(Person &&rhs); Person& operator=(const Person &rhs); Person& operator

r-value reference return type semantics?

蓝咒 提交于 2019-12-23 08:38:15
问题 Does a return type like this represent something meaningful in c++11? template <typename R> R&& grabStuff(); T instance = grabStuff<T>(); I would hope that grabStuff should throw a compile-time error if R does not have a move constructor, since this would seem to disallow the return type to use a copy constructor 回答1: As always, when returning references you must return a reference to something that's still alive after the function returns. How you do that is up to you. Example: T global

non-const reference of type from an rvalue

你。 提交于 2019-12-23 07:26:09
问题 Consider the following code: class Widget{}; template<typename T> T &&foo2(T &&t){ return std::forward<T>( t ); } /// Return 1st element template<typename T> typename std::tuple_element<0, typename std::decay<T>::type >::type &&foo(T &&t){ return std::forward< typename std::tuple_element<0, typename std::decay<T>::type >::type > ( std::get<0>(t) ); } Widget w; auto list = std::make_tuple( w, Widget() ); int main() { auto &l = foo(list ); // This is NOT work //auto &l2 = foo2( std::get<0>(list

non-const reference of type from an rvalue

守給你的承諾、 提交于 2019-12-23 07:25:00
问题 Consider the following code: class Widget{}; template<typename T> T &&foo2(T &&t){ return std::forward<T>( t ); } /// Return 1st element template<typename T> typename std::tuple_element<0, typename std::decay<T>::type >::type &&foo(T &&t){ return std::forward< typename std::tuple_element<0, typename std::decay<T>::type >::type > ( std::get<0>(t) ); } Widget w; auto list = std::make_tuple( w, Widget() ); int main() { auto &l = foo(list ); // This is NOT work //auto &l2 = foo2( std::get<0>(list

C++11 - Distinguishing rvalue pointers

不羁岁月 提交于 2019-12-22 18:21:21
问题 How can I distinguish a variable as a compiler-constructed string? For example, while the rvalue "Hello, World" is of type const char* . const char* in itself does not mean that a pointer can't be changed. A char* const pointer can't be changed, but that's not what's constructed by the compiler. Does this mean that, for any container that holds a const char* , the data should be copied by means other than C++'s move semantics? Is there any way to just move compiler-constructed strings and

Why reference can not capture temporary while const ref and rval ref can [duplicate]

风格不统一 提交于 2019-12-22 17:40:13
问题 This question already has answers here : How come a non-const reference cannot bind to a temporary object? (11 answers) Closed 5 years ago . Why reference can not capture temporary value while const reference and rvalue reference can capture and prolong object life. In other words while two first lines are legal but third not: const string &a = string("a"); string &&b = string("b"); string &c = string("c"); // why illegal? 回答1: Quoting from N1377 Bjarne in his excellent text "The Design and

C++11 Move semantics behaviour specific questions

我是研究僧i 提交于 2019-12-22 16:09:13
问题 I have read the below post which gives a very good insight into move semantics: Can someone please explain move semantics to me? but I am still fail to understand following things regarding move semantics - Does copy elision and RVO would still work for classes without move constructors? Even if our classes doesn't have move constructors, but STL containers has one. For operation like std::vector<MyClass> vt = CreateMyClassVector(); and to perform operations like sorting etc. Why can't STL

C++11 Move semantics behaviour specific questions

萝らか妹 提交于 2019-12-22 16:09:06
问题 I have read the below post which gives a very good insight into move semantics: Can someone please explain move semantics to me? but I am still fail to understand following things regarding move semantics - Does copy elision and RVO would still work for classes without move constructors? Even if our classes doesn't have move constructors, but STL containers has one. For operation like std::vector<MyClass> vt = CreateMyClassVector(); and to perform operations like sorting etc. Why can't STL

Why can an rvalue not bind to a non-const lvalue reference, other than the fact that writing to a temporary has no effect?

亡梦爱人 提交于 2019-12-22 11:12:06
问题 I have read the SO question here and understood this part of the answer: "But if you bind a temporary to a non-const reference, you can keep passing it around "forever" just to have your manipulation of the object disappear, because somewhere along the way you completely forgot this was a temporary." That is, in the following: #include <iostream> void modifyValue(int& rValue) { rValue++; } int main() { modifyValue(9899); return 0; } If an rvalue could bind to a non-const lvalue reference,

move constructor and std::move confusion

别说谁变了你拦得住时间么 提交于 2019-12-22 04:13:19
问题 I am reading about the std::move, move constructor and move assignment operator. To be honest, all I got now is confusion. Now I have a class: class A{ public: int key; int value; A(){key = 3; value = 4;} //Simple move constructor A(A&& B){ A.key = std::move(B.key); A.value = std::move(B.value);} }; I thought B is an rvalue reference, why you can apply std::move to an ravlue reference's member? After B.key and B.value have been moved, both have been invalidated, but how B as an object of