move-constructor

(Why) should a move constructor or move assignment operator clear its argument?

我是研究僧i 提交于 2019-12-05 08:28:31
问题 An example move constructor implementation from a C++ course I’m taking looks a bit like this: /// Move constructor Motorcycle::Motorcycle(Motorcycle&& ori) : m_wheels(std::move(ori.m_wheels)), m_speed(ori.m_speed), m_direction(ori.m_direction) { ori.m_wheels = array<Wheel, 2>(); ori.m_speed = 0.0; ori.m_direction = 0.0; } ( m_wheels is a member of type std::array<Wheel, 2> , and Wheel only contains a double m_speed and a bool m_rotating . In the Motorcycle class, m_speed and m_direction are

How is the C++ synthesized move constructor affected by volatile and virtual members?

孤街浪徒 提交于 2019-12-04 18:37:27
问题 Look at the following code: struct node { node(); //node(const node&); //#1 //node(node&&); //#2 virtual //#3 ~node (); node* volatile //#4 next; }; int main() { node m(node()); //#5 node n=node(); //#6 } When compiled with gcc-4.6.1 it produces the following error: g++ -g --std=c++0x -c -o node.o node.cc node.cc: In constructor node::node(node&&): node.cc:3:8: error: expression node::next has side-effects node.cc: In function int main(): node.cc:18:14: note: synthesized method node::node

Why do we need to set rvalue reference to null in move constructor?

一笑奈何 提交于 2019-12-04 03:07:27
//code from https://skillsmatter.com/skillscasts/2188-move-semanticsperfect-forwarding-and-rvalue-references class Widget { public: Widget(Widget&& rhs) : pds(rhs.pds) // take source’s value { rhs.pds = nullptr; // why?? } private: struct DataStructure; DataStructure *pds; }; I can't understand the reason for setting rhd.pds to nullptr . What will happen if we remove this line : rhs.pds = nullptr; Joseph Mansfield Some details of the class have been removed. In particular, the constructor dynamically allocates the DataStructure object and the destructor deallocates it. If, during a move, you

(Why) should a move constructor or move assignment operator clear its argument?

北慕城南 提交于 2019-12-03 23:27:35
An example move constructor implementation from a C++ course I’m taking looks a bit like this: /// Move constructor Motorcycle::Motorcycle(Motorcycle&& ori) : m_wheels(std::move(ori.m_wheels)), m_speed(ori.m_speed), m_direction(ori.m_direction) { ori.m_wheels = array<Wheel, 2>(); ori.m_speed = 0.0; ori.m_direction = 0.0; } ( m_wheels is a member of type std::array<Wheel, 2> , and Wheel only contains a double m_speed and a bool m_rotating . In the Motorcycle class, m_speed and m_direction are also double s.) I don’t quite understand why ori ’s values need to be cleared. If a Motorcycle had any

How is the C++ synthesized move constructor affected by volatile and virtual members?

拥有回忆 提交于 2019-12-03 12:14:20
Look at the following code: struct node { node(); //node(const node&); //#1 //node(node&&); //#2 virtual //#3 ~node (); node* volatile //#4 next; }; int main() { node m(node()); //#5 node n=node(); //#6 } When compiled with gcc-4.6.1 it produces the following error: g++ -g --std=c++0x -c -o node.o node.cc node.cc: In constructor node::node(node&&): node.cc:3:8: error: expression node::next has side-effects node.cc: In function int main(): node.cc:18:14: note: synthesized method node::node(node&&) first required here As I understand the compiler fails to create default move or copy constructor

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

Does rule of not embedding std::string in exceptions still hold with move constructors?

≯℡__Kan透↙ 提交于 2019-12-03 02:21:46
I heard some time ago that I should not create exception classes which would have fields of std::string type. That's what Boost website says . The rationale is that std::string copy constructor can throw an exception if memory allocation fails, and if an exception is thrown before the currently processed exception is caught, the program is terminated. However, does it still hold in the world of move constructors? Won't the move constructor be used instead of the copy constructor when throwing an exception? Do I understand correctly that with C++11 no memory allocation will take place, no

How can I check if a move constructor is being generated implicitly?

走远了吗. 提交于 2019-12-03 01:46:36
I have several classes for which I wish to check whether a default move constructor is being generated. Is there a way to check this (be it a compile-time assertion, or parsing the generated object files, or something else)? Motivational example: class MyStruct : public ComplicatedBaseClass { std::vector<std::string> foo; // possibly huge ComplicatedSubObject bar; }; If any member of any base or member of either Complicated...Object classes cannot be moved, MyStruct will not have its implicit move constructor generated, and may thus fail to optimize away the work of copying foo , when a move

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

泄露秘密 提交于 2019-12-02 20:23:45
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? 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 will either be a copy-constructor or move-constructor. From there, you just move the function parameter values