rvalue-reference

Best form for constructors? Pass by value or reference?

丶灬走出姿态 提交于 2019-12-02 16:23:44
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 into x1.m_y (2 copies) (1c) y is moved into x1.m_y (1 move) (2a) result of f() is copied into x2.m_y (1

Workarounds for no 'rvalue references to *this' feature

此生再无相见时 提交于 2019-12-02 15:20:50
I have a proxy container class around a movable object, and wish the proxy to be able to implicitly yield an rvalue reference to the underlying object, but only when the proxy itself is being moved. I believe that I will be able to implement this behaviour as per proposal n2439 "Extending move semantics to *this" , but it is not yet available in a release of gcc and won't be for a while. The code below is what I am ultimately aiming for, but is not currently possible. Until this feature is available to me, are there any equivalent workarounds? template< class T > struct movable_proxy {

std::is_rvalue_reference inside template function with rvalue reference parameter

∥☆過路亽.° 提交于 2019-12-02 06:52:18
问题 #include<iostream> #include<vector> struct Empty {}; template <typename V> void add(V&& element) { static_assert( std::is_rvalue_reference<V>::value, "V is not a rvalue reference"); } int main(int argc, char *argv[]) { add(Empty()); std::cin.ignore(); return 0; } I don't get why static_assert fail here, V isn't equal to V&& here ? 回答1: V is not the rvalue-reference - decltype(element) is (if element is an rvalue). V is simply the general type of element . Specifically: typename std::remove

std::is_rvalue_reference inside template function with rvalue reference parameter

与世无争的帅哥 提交于 2019-12-02 00:56:14
#include<iostream> #include<vector> struct Empty {}; template <typename V> void add(V&& element) { static_assert( std::is_rvalue_reference<V>::value, "V is not a rvalue reference"); } int main(int argc, char *argv[]) { add(Empty()); std::cin.ignore(); return 0; } I don't get why static_assert fail here, V isn't equal to V&& here ? V is not the rvalue-reference - decltype(element) is (if element is an rvalue). V is simply the general type of element . Specifically: typename std::remove_reference<decltype(element)>::type; // V 来源: https://stackoverflow.com/questions/19753450/stdis-rvalue

move semantics std::move how use it

回眸只為那壹抹淺笑 提交于 2019-12-01 21:03:19
#include <type_traits> template<class T> typename std::remove_reference<T>::type&& move(T&& v) { return v; } void main() { int a; move(a); } Why doesn't this code compile? error C2440: 'return' : impossible to convert 'int' in 'int &&' This is straight out of the C++0x draft standard (§20.2.3/6): template <class T> typename remove_reference<T>::type&& move(T&& t) noexcept; Returns : static_cast<typename remove_reference<T>::type&&>(t) . Consequently, if you change your move implementation to the following, it works just fine: template<class T> typename std::remove_reference<T>::type&& move(T&&

Apparently missing overload of getline() taking RRef to stream in GCC 4.7.2 and Clang 3.2

*爱你&永不变心* 提交于 2019-12-01 20:36:50
I ran into an unexpected compilation error when trying to use getline() with a temporary stream object: #include <iostream> #include <string> #include <sstream> using namespace std; int main() { string input = "hello\nworld\nof\ndelimiters"; string line; if (getline(stringstream(input), line)) // ERROR! { cout << line << endl; } } It looks like no overload of getline() exists that accepts an rvalue reference to a stream object. If I change main() to use an lvalue, it compiles and runs as expected: int main() { string input = "hello\nworld\nof\ndelimiters"; string line; stringstream ss(inpupt);

How to avoid the copy when I return

 ̄綄美尐妖づ 提交于 2019-12-01 16:58:02
I have a function which returns a vector or set: set<int> foo() { set<int> bar; // create and massage bar return bar; } set<int> afoo = foo(); In this case, I create a temporary memory space in function foo(), and then assign it to afoo by copying. I really want to avoid this copy, any easy way I can do this in C++11? I think this has to do with the rvalue thing. OK, update to the question: If I am going to return an object defined by myself, not the vector or set thing, does that mean I should define a move constructor? like this: class value_to_return { value_to_return (value_to_return &&

C++ universal reference in constructor and return value optimization (rvo)

狂风中的少年 提交于 2019-12-01 16:55:42
Why does rvalue optimization not occur in classes with constructor with universal reference arguments? http://coliru.stacked-crooked.com/a/672f10c129fe29a0 #include <iostream> template<class ...ArgsIn> struct C { template<class ...Args> C(Args&& ... args) {std::cout << "Ctr\n";} // rvo occurs without && ~C(){std::cout << "Dstr\n";} }; template<class ...Args> auto f(Args ... args) { int i = 1; return C<>(i, i, i); } int main() { auto obj = f(); } Output: Ctr Ctr Dstr Ctr Dstr Dstr I believe that the problem is that instantiations of template<class ...Args> C(Args&& ... args) {std::cout << "Ctr

Are moved-from objects required to be destructed?

旧巷老猫 提交于 2019-12-01 16:19:46
If I move-construct a from b , is it still necessary to destruct b , or can I get away without doing so? This question crossed my mind during the implementation of an optional<T> template. Excerpt: ~optional() { if (initialized) { reinterpret_cast<T*>(data)->~T(); } } optional(optional&& o) : initialized(o.initialized) { if (initialized) { new(data) T(std::move(*o)); // move from o.data o.initialized = false; // o.data won't be destructed anymore! } } Of course, I could just replace the bool initialized with a three-valued enumeration that distinguishes between initialized, non-initialized and

How to avoid the copy when I return

喜夏-厌秋 提交于 2019-12-01 15:15:32
问题 I have a function which returns a vector or set: set<int> foo() { set<int> bar; // create and massage bar return bar; } set<int> afoo = foo(); In this case, I create a temporary memory space in function foo(), and then assign it to afoo by copying. I really want to avoid this copy, any easy way I can do this in C++11? I think this has to do with the rvalue thing. OK, update to the question: If I am going to return an object defined by myself, not the vector or set thing, does that mean I