rvalue-reference

Is it useless to declare a local variable as rvalue-reference, e.g. T&& r = move(v)?

与世无争的帅哥 提交于 2019-12-03 09:33:59
Could you guys give me an illustrative example under certain circumstance to prove the following statements are useful and necessary? AnyTypeMovable v; AnyTypeMovable&& r = move(v); Nemanja Boric No, AnyTypeMovable&& r = move(v); here is not useful at all. Consider the following code: #include <iostream> #include <vector> class MyMovableType { int i; public: MyMovableType(int val): i(val){} MyMovableType(MyMovableType&& r) { this->i = r.i; r.i = -1; } MyMovableType(const MyMovableType& r){ this->i = r.i; } int getVal(){ return i; } }; int main() { std::vector<MyMovableType> vec; MyMovableType

Forwarding of return values. Is std::forward is needed?

落花浮王杯 提交于 2019-12-03 09:06:09
问题 I am writing library which wraps a lot of functions and methods from other library. To avoid coping of return values I am applying std::forward like so: template<class T> T&& wrapper(T&& t) { f(t); // t passed as lvalue return std::forward<T>(t); } f returns void and takes T&& (or overloaded on valueness). Wrapper always returns wrappers's param and on returned value should preserve valuness of argument. Do I actually need to use std::forward in return ? Does RVO makes it superfluous? Does

rvalue refs and std::move

被刻印的时光 ゝ 提交于 2019-12-03 09:00:34
Can someone explain why B doesn't compile, but C does? I don't understand why std::move is required since the variable is already an rvalue ref. struct A { int x; A(int x=0) : x(x) {} A(A&& a) : x(a.x) { a.x = 0; } }; struct B : public A { B() {} B(B&& b) : A(b) {} // compile error with g++-4.7 }; struct C : public A { C() {} C(C&& c) : A(std::move(c)) {} // ok, but why? }; In the statement: B(B&& b) The parameter b is declared with the type: rvalue reference to B . In the statement: A(b) The expression b is an lvalue of type B . And lvalue expressions can not bind to rvalue references:

Why is it not efficient to use a single assignment operator handling both copy and move assignment?

て烟熏妆下的殇ゞ 提交于 2019-12-03 07:54:48
问题 Here is an exercise from C++ Primer 5th Edition : Exercise 13.53: As a matter of low-level efficiency, the HasPtr assignment operator is not ideal. Explain why. Implement a copy-assignment and move-assignment operator for HasPtr and compare the operations executed in your new move-assignment operator versus the copy-and-swap version.(P.544) File hasptr.h : //! a class holding a std::string* class HasPtr { friend void swap(HasPtr&, HasPtr&); friend bool operator <(const HasPtr& lhs, const

Passing/Moving parameters of a constructor in C++0x

纵饮孤独 提交于 2019-12-03 06:44:38
问题 If I have a constructor with n parameters such that any argument to that can be an rvalue and lvalue. Is it possible to do support this with move semantics for the rvalues without writing 2^n constructors for each possible rvalue/lvalue combination? 回答1: You take each one by value, like this: struct foo { foo(std::string s, bar b, qux q) : mS(std::move(s)), mB(std::move(b)), mQ(std::move(q)) {} std::string mS; bar mB; qux mQ; }; The initialization of the function parameters by the argument

Move Constructors and Static Arrays

自古美人都是妖i 提交于 2019-12-03 06:42:16
问题 I've been exploring the possibilities of Move Constructors in C++, and I was wondering what are some ways of taking advantage of this feature in an example such as below. Consider this code: template<unsigned int N> class Foo { public: Foo() { for (int i = 0; i < N; ++i) _nums[i] = 0; } Foo(const Foo<N>& other) { for (int i = 0; i < N; ++i) _nums[i] = other._nums[i]; } Foo(Foo<N>&& other) { // ??? How can we take advantage of move constructors here? } // ... other methods and members virtual

About catching exception good practices

浪尽此生 提交于 2019-12-03 06:09:17
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{"what"}; } catch (const std::exception& ex) {} 3) try { throw std::exception{"what"}; } catch (std:

C++0x rvalue references and temporaries

瘦欲@ 提交于 2019-12-03 06:01:09
问题 (I asked a variation of this question on comp.std.c++ but didn't get an answer.) Why does the call to f(arg) in this code call the const ref overload of f ? void f(const std::string &); //less efficient void f(std::string &&); //more efficient void g(const char * arg) { f(arg); } My intuition says that the f(string &&) overload should be chosen, because arg needs to be converted to a temporary no matter what, and the temporary matches the rvalue reference better than the lvalue reference.

Are there any use cases for std::forward with a prvalue?

对着背影说爱祢 提交于 2019-12-03 05:40:31
问题 The most common usage of std::forward is to, well, perfect forward a forwarding (universal) reference, like template<typename T> void f(T&& param) { g(std::forward<T>(param)); // perfect forward to g } Here param is an lvalue , and std::forward ends up casting it to a rvalue or lvalue, depending on what the argument that bounded to it was. Looking at the definition of std::forward from cppreference.com I see that there is also a rvalue overload template< class T > T&& forward( typename std:

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