move-semantics

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

血红的双手。 提交于 2019-12-02 21:19:52
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 HasPtr& rhs); public: //! default constructor. HasPtr(const std::string &s = std::string()): ps(new std:

Move Constructors and Static Arrays

落爺英雄遲暮 提交于 2019-12-02 20:21:01
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 ~Foo() { /* no action required */ } private: int _nums[N]; }; Foo<5> bar() { Foo<5> result; // Do stuff

C++11: write move constructor with atomic<bool> member?

时光怂恿深爱的人放手 提交于 2019-12-02 20:20:48
I've got a class with an atomic member variable: struct Foo { std::atomic<bool> bar; /* ... lots of other stuff, not relevant here ... */ Foo() : bar( false ) {} /* Trivial implementation fails in gcc 4.7 with: * error: use of deleted function ‘std::atomic<bool>::atomic(const td::atomic<bool>&)’ */ Foo( Foo&& other ) : bar( other.bar ) {} }; Foo f; Foo f2(std::move(f)); // use the move How should be move constructor look like? Gcc 4.7 doesn't like any of my attempts (like adding std::move() around the other.bar ) and the net is surprisingly quiet here... Since you're moving other , no one else

Efficient use of move semantics together with (N)RVO

可紊 提交于 2019-12-02 19:49:31
Let's say I want to implement a function that is supposed to process an object and return a new (possibly changed) object. I would like to do this as efficient as possible in C+11. The environment is as follows: class Object { /* Implementation of Object */ Object & makeChanges(); }; The alternatives that come to my mind are: // First alternative: Object process1(Object arg) { return arg.makeChanges(); } // Second alternative: Object process2(Object const & arg) { return Object(arg).makeChanges(); } Object process2(Object && arg) { return std::move(arg.makeChanges()); } // Third alternative:

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

大兔子大兔子 提交于 2019-12-02 19:01:15
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::remove_reference<T>::type&& t ); Can anyone give me any reason why the rvalue overload? I cannot see any

Correct use of `= delete` for methods in classes

白昼怎懂夜的黑 提交于 2019-12-02 17:05:51
Is the following snipplet correct for un-defining all otherwise generated methods and constructors for a class? struct Picture { // 'explicit': no accidental cast from string to Picture explicit Picture(const string &filename) { /* load image from file */ } // no accidental construction, i.e. temporaries and the like Picture() = delete; // no copy Picture(const Picture&) = delete; // no assign Picture& operator=(const Picture&) = delete; // no move Picture(Picture&&) = delete; // no move-assign Picture& operator=(Picture&&) = delete; // return type correct? }; This deletes every default

Is std::move really needed on initialization list of constructor for heavy members passed by value?

给你一囗甜甜゛ 提交于 2019-12-02 17:04:37
Recently I read an example from cppreference.../vector/emplace_back : struct President { std::string name; std::string country; int year; President(std::string p_name, std::string p_country, int p_year) : name(std::move(p_name)), country(std::move(p_country)), year(p_year) { std::cout << "I am being constructed.\n"; } My question: is this std::move really needed? My point is that this p_name is not used in the body of constructor, so, maybe, there is some rule in the language to use move semantics for it by default? That would be really annoying to add std::move on initialization list to every

Why is passing by value (if a copy is needed) recommended in C++11 if a const reference only costs a single copy as well?

北慕城南 提交于 2019-12-02 16:37:21
I am trying to understand move semantics, rvalue references, std::move , etc. I have been trying to figure out, by searching through various questions on this site, why passing a const std::string &name + _name(name) is less recommended than a std::string name + _name(std::move(name)) if a copy is needed. If I understand correctly, the following requires a single copy (through the constructor) plus a move (from the temporary to the member): Dog::Dog(std::string name) : _name(std::move(name)) {} The alternative (and old-fashioned) way is to pass it by reference and copy it (from the reference

Const reference VS move semantics

▼魔方 西西 提交于 2019-12-02 15:32:45
I was wondering in which situations I still need to use const references in parameters since C++11. I don't fully understand move semantics but I think this is a legit question. This question is meant for only the situations where a const reference replaces a copy being made while it is only needed to "read" the value (e.g. usage of const member functions). Normally I would write a (member)function like this: #include <vector> template<class T> class Vector { std::vector<T> _impl; public: void add(const T& value) { _impl.push_back(value); } }; But I'm thinking that it's safe to assume that to

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 {