move-semantics

Generic conversion operator templates and move semantics: any universal solution?

六月ゝ 毕业季﹏ 提交于 2019-12-03 10:39:25
This is a follow-up of Explicit ref-qualified conversion operator templates in action . I have experimented with many different options and I am giving some results here in an attempt to see if there is any solution eventually. Say a class (e.g. any ) needs to provide conversion to any possible type in a convenient, safe (no surprises) way that preserves move semantics. I can think of four different ways. struct A { // explicit conversion operators (nice, safe?) template<typename T> explicit operator T&& () &&; template<typename T> explicit operator T& () &; template<typename T> explicit

Why doesn't `std::stringstream::stringstream(std::string&&)` exist?

十年热恋 提交于 2019-12-03 10:27:08
问题 I was hoping stringstream has a constructor that steals its initial content from a string&& . Do such inter-species "move constructors" generally not exist in the STL? If not, why not? 回答1: There's history, which is disappointing. But also a future that looks bright. When move semantics went into C++11, it was huge, controversial, and overwhelming. I wanted to be able to move strings into and out of stringstream . However the politics at the time demanded that the internal store did not have

Move-assignment and reference member

巧了我就是萌 提交于 2019-12-03 10:27:00
Copy-assignment for a class with a reference member variable is a no-no because you can't reassign the reference. But what about move-assignment? I tried simply move ing it but, of course, that destroys the source object when I just want to move the reference itself: class C { public: C(X& x) : x_(x) {} C(C&& other) : x_(std::move(other.x_)) {} C& operator=(C&& other) { x_ = std::move(other.x_); } private: X& x_; }; X y; C c1(y); X z; C c2(z); c2 = c1; // destroys y as well as z Should I just not be implementing move-assignment and sticking with move-construction only? That makes swap(C&, C&)

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

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

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

(Missing) performance improvements with C++11 move semantics

北城以北 提交于 2019-12-03 06:30:34
问题 I've been writing C++11 code for quite some time now, and haven't done any benchmarking of it, only expecting things like vector operations to "just be faster" now with move semantics. So when actually benchmarking with GCC 4.7.2 and clang 3.0 (default compilers on Ubuntu 12.10 64-bit) I get very unsatisfying results. This is my test code: EDIT: With regards to the (good) answers posted by @DeadMG and @ronag, I changed the element type from std::string to my::string which does not have a swap

Constructor using std::forward

岁酱吖の 提交于 2019-12-03 05:55:09
问题 To my knowledge, the two common ways of efficiently implementing a constructor in C++11 are using two of them Foo(const Bar& bar) : bar_{bar} {}; Foo(Bar&& bar) : bar_{std::move(bar)} {}; or just one in the fashion of Foo(Bar bar) : bar_{std::move(bar)} {}; with the first option resulting in optimal performance (e.g. hopefully a single copy in case of an lvalue and a single move in case of an rvalue), but needing 2 N overloads for N variables, whereas the second option only needs one function

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:

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

亡梦爱人 提交于 2019-12-03 05:36:30
问题 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()