move-semantics

In what scenarios should I expect to explicitly need to implement a move constructor and move assignment operator?

浪尽此生 提交于 2019-12-17 22:40:41
问题 Given that a class actually is moveable , manually implementing the move constructor and move assignment operator for a class quickly become tedious. I was wondering when doing so is actually a heavy, heavy, premature optimization? For instance, if a class only has trivial POD data or members that themselves have move constructor and move assignment operator defined, then I'd guess that the compiler will either just optimize the shit out of the lot (in the case of PODs) and otherwise use the

Initializer-list-constructing a vector of noncopyable (but movable) objects

送分小仙女□ 提交于 2019-12-17 19:57:12
问题 One can push_back rvalues of a noncopyable-but-movable type into a vector of that type: #include <vector> struct S { S(int); S(S&&); }; int main() { std::vector<S> v; v.push_back(S(1)); v.push_back(S(2)); v.push_back(S(3)); } However, when I try to initializer-list-construct the vector with the same rvalues, I get errors about a copy constructor being required: #include <vector> struct S { S(int); S(S&&); }; int main() { std::vector<S> v = {S(1), S(2), S(3)}; } I get the following errors with

visual studio implementation of “move semantics” and “rvalue reference”

余生长醉 提交于 2019-12-17 19:46:52
问题 I came across a Youtube video on c++11 concurrency (part 3) and the following code, which compiles and generates correct result in the video. However, I got a compile error of this code using Visual Studio 2012. The compiler complains about the argument type of toSin(list<double>&&) . If I change the argument type to list<double>& , the code compiled. My question is what is returned from move(list) in the _tmain() , is it a rvalue reference or just a reference? #include "stdafx.h" #include

Passing by value vs const & and && overloads

扶醉桌前 提交于 2019-12-17 17:43:08
问题 So after looking up move semantics I see that general consensus is to pass by value when you intend to transfer ownership. But in Scott Meyer's talk on Universal references I've noticed that std::vector::push_back has 2 overloads: void push_back( const T& value ); void push_back( T&& value ); So I thought to myself, wouldn't void push_back( T value ); be enough? I've asked a few people which ultimately lead to the following test case: #include <memory> #include <iostream> #include <type

Is the default Move constructor defined as noexcept?

我怕爱的太早我们不能终老 提交于 2019-12-17 17:35:05
问题 It seems that a vector will check if the move constructor is labeled as noexcept before deciding on whether to move or copy elements when reallocating. Is the default move constructor defined as noexcept? I saw the following documentation but it didn't specify this.http://en.cppreference.com/w/cpp/language/move_constructor Implicitly-declared move constructor If no user-defined move constructors are provided for a class type (struct, class, or union), and all of the following is true: there

Move semantics & returning const values

隐身守侯 提交于 2019-12-17 16:41:47
问题 I have the habit (?!?!?) of returning everything as a "const" value. Like this... struct s; s const make_s(); s const &s0 = make_s(); s const s1 = make_s(); With move operations and r-value references and the following functions... void take_s(s &&s0); void take_s(s const &&s0); // Doesn't make sense I can no longer write... take_s(make_s()); The main reason I started using the convention of returning const values is to prevent someone from writing code like this... make_s().mutating_member

Is returning with `std::move` sensible in the case of multiple return statements?

混江龙づ霸主 提交于 2019-12-17 10:41:27
问题 I'm aware that it's normally not a good idea to return with std::move , i.e. bigObject foo() { bigObject result; /*...*/ return std::move(result); } instead of simply bigObject foo() { bigObject result; /*...*/ return result; } because it gets in the way of return value optimization. But what in the case of a function with multiple different returns, particularly something like class bar { bigObject fixed_ret; bool use_fixed_ret; void prepare_object(bigObject&); public: bigObject foo() { if

Default move constructor/assignment and deleted copy constructor/assignment

蹲街弑〆低调 提交于 2019-12-17 09:39:53
问题 According to the standard, If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if — X does not have a user-declared copy constructor, — X does not have a user-declared copy assignment operator, — X does not have a user-declared move assignment operator, and — X does not have a user-declared destructor. Now the following fails to compile # include <utility> class Foo { public: Foo() = default; Foo(Foo const &)

Can I typically/always use std::forward instead of std::move?

孤人 提交于 2019-12-17 06:24:08
问题 I've been watching Scott Meyers' talk on Universal References from the C++ and Beyond 2012 conference, and everything makes sense so far. However, an audience member asks a question at around 50 minutes in that I was also wondering about. Meyers says that he does not care about the answer because it is non-idiomatic and would silly his mind, but I'm still interested. The code presented is as follows: // Typical function bodies with overloading: void doWork(const Widget& param) // copy { //

How to actually implement the rule of five?

我的未来我决定 提交于 2019-12-17 05:40:49
问题 UPDATE at the bottom q1: How would you implement the rule of five for a class that manages rather heavy resources, but of which you want it to be passed around by value because that greatly simplifies and beautifies it's usage? Or are not all five items of the rule even needed? In practice, I'm starting something with 3D imaging where an image is usually 128*128*128 doubles. Being able though to write things like this would make the math alot easier: Data a = MakeData(); Data c = 5 * a + ( 1