rvalue-reference

Take ownership of parameter by rvalue-reference

让人想犯罪 __ 提交于 2019-12-05 00:29:40
I want to make clear that the constructor of my class A will take ownership of the passed Data parameter. The obvious thing to do is take a unique_ptr by value: class A { public: A(std::unique_ptr<Data> data) : _data(std::move(data)) { } std::unique_ptr<Data> _data; }; However, for my use-case, there is no reason why Data should be a pointer, since a value type would suffice. The only remaining option that I could think of to make really clear that Data will be owned by A is pass by rvalue-reference: class A { public: A(Data&& data) : _data(std::move(data)) { } Data _data; }; Is this a valid

Can an rvalue reference bind to a function?

帅比萌擦擦* 提交于 2019-12-05 00:17:55
I tested the following code with GCC, Clang, ICC and VS: void f() {} void g(void (&&)()) { } int main() { g(f); } As we can see, g takes an rvalue reference but f is an lvalue and, in general, rvalue references cannot be bound to lvalues. That's exactly what ICC complains about: error: an rvalue reference cannot be bound to an lvalue VS also gives an error but for another reason: error C2664: 'void h(void (__cdecl &&)(void))' : cannot convert parameter 1 from 'void (__cdecl *)(void)' to 'void (__cdecl &&)(void)' This suggests to me that VS is immediately performing a function-to-pointer

Why this two rvalue references examples have different behavior?

安稳与你 提交于 2019-12-05 00:16:50
问题 First example int a = 0; auto && b = ++a; ++a; cout << a << b << endl; prints 22 Second example int a = 0; auto && b = a++; ++a; cout << a << b << endl; prints 20 Question: Why in first example ++a in 3rd line also increments b , and why there is no such behavior in second example? Update: New question arised. 回答1: Because pre-increment ( ++a ) first increments the value of a , stores the result, and then returns the reference to a . Now a and b effectively point to the same object. Post

Why would const-ness of a local variable inhibit move semantics for the returned value?

瘦欲@ 提交于 2019-12-04 23:07:50
struct STest : public boost::noncopyable { STest(STest && test) : m_n( std::move(test.m_n) ) {} explicit STest(int n) : m_n(n) {} int m_n; }; STest FuncUsingConst(int n) { STest const a(n); return a; } STest FuncWithoutConst(int n) { STest a(n); return a; } void Caller() { // 1. compiles just fine and uses move ctor STest s1( FuncWithoutConst(17) ); // 2. does not compile (cannot use move ctor, tries to use copy ctor) STest s2( FuncUsingConst(17) ); } The above example illustrates how in C++11, as implemented in Microsoft Visual C++ 2012, the internal details of a function can modify its

C++, rvalue references in function parameters

浪子不回头ぞ 提交于 2019-12-04 16:26:59
问题 I'm trying to understand rvalue references. I have seen how they are used in constructors, with things like std::move and std::forward , but I still don't understand why this doesn't work: void func(string&& str) { cout << str << endl; } int main(int argc, char* argv[]) { string s("string"); func(s); } And this does: template<typename T> void func(T&& str) { cout << str << endl; } int main(int argc, char* argv[]) { string s("string"); func(s); } Why does it work with the function template

C++ “move from” container

北城以北 提交于 2019-12-04 16:03:06
问题 In C++11, we can get an efficiency boost by using std::move when we want to move (destructively copy) values into a container: SomeExpensiveType x = /* ... */; vec.push_back(std::move(x)); But I can't find anything going the other way. What I mean is something like this: SomeExpensiveType x = vec.back(); // copy! vec.pop_back(); // argh This is more frequent (the copy-pop) on adapter's like stack . Could something like this exist: SomeExpensiveType x = vec.move_back(); // move and pop To

rvalue refs and std::move

时光怂恿深爱的人放手 提交于 2019-12-04 15:48:10
问题 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? }; 回答1: In the statement: B(B&& b) The parameter b is declared with the type: rvalue reference to B . In the statement: A(b

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

て烟熏妆下的殇ゞ 提交于 2019-12-04 15:20:07
问题 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); 回答1: 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

rvalue reference with assignement operator

时光怂恿深爱的人放手 提交于 2019-12-04 14:28:45
In this article http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1877 : T& T::operator=(T const& x) // x is a reference to the source { T tmp(x); // copy construction of tmp does the hard work swap(*this, tmp); // trade our resources for tmp's return *this; // our (old) resources get destroyed with tmp } but in light of copy elision, that formulation is glaringly inefficient! It’s now “obvious” that the correct way to write a copy-and-swap assignment is: T& operator=(T x) // x is a copy of the source; hard work already done { swap(*this, x); // trade our

C++ constructor with rvalue reference

十年热恋 提交于 2019-12-04 14:12:51
问题 Consider this class with three constructors: class Circle { public: Circle(int r) { _radius = r; } Circle(const Circle& c){ _radius = c.radius(); cout << endl << "Copy constructor with lvalue reference. Radius: " << _radius; } Circle(Circle&& c){ _radius = c.radius(); cout << endl << "Copy constructor with rvalue reference. Radius:" << _radius; } int radius() const { return _radius; } private: int _radius; }; int main() { Circle c1(2); Circle c2(c1); cout << endl << c2.radius(); Circle c3