move-constructor

Implicit move vs copy operations and containment

前提是你 提交于 2020-07-05 12:27:25
问题 I am struggling to understand implicit move operations when a class has a member whose move operations were not defined: int main() { struct A // no move: move = copy { A() = default; A(const A&) { cout << "A'copy-ctor\n"; }; A& operator=(const A&) { cout << "A'copy-assign\n"; return *this; } }; struct B { B() = default; A a; // does this make B non-moveable? unique_ptr<int> upi; // B(B&&) noexcept = default; // B& operator=(B&&)noexcept = default; }; A a; A a2 = std::move(a); // ok use copy

move Constructor is not called

自闭症网瘾萝莉.ら 提交于 2020-01-23 13:13:22
问题 I am implementing a IntArray Class for learning C++. I must admit I haven't fully understood r and lvalues and move constructors, yet. I wanted to try it out to see if my code is working, but I do not know why {IntArray array = IntArray(5);} doesn't call my implemented move constructor. I thought this would be a case for it. #include "IntArray.h" IntArray::IntArray() :data(nullptr), count(0), capacity(0) {std::cout << "Default Constructor called" << std::endl;} IntArray::IntArray(int size)

Move constructor/operator=

时间秒杀一切 提交于 2020-01-13 07:21:10
问题 I'm trying to learn about new feature of C++ namely move constructor and assignment X::operator=(X&&) and I found interesting example but the only thing I quite not even understand but more dissagree is one line in move ctor and assignment operator (marked in the code below): MemoryBlock(MemoryBlock&& other) : _data(NULL) , _length(0) { std::cout << "In MemoryBlock(MemoryBlock&&). length = " << other._length << ". Moving resource." << std::endl; // Copy the data pointer and its length from

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

吃可爱长大的小学妹 提交于 2020-01-12 03:15:24
问题 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 object without a move constructor

∥☆過路亽.° 提交于 2020-01-03 19:16:30
问题 One more time about this, but the related questions do not answer my question. The standard is pretty clear: 12.8 Copying and moving class objects, §9 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, — X does not have a user-declared

Move object without a move constructor

怎甘沉沦 提交于 2020-01-03 19:16:10
问题 One more time about this, but the related questions do not answer my question. The standard is pretty clear: 12.8 Copying and moving class objects, §9 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, — X does not have a user-declared

Move Constructor calling base-class Move Constructor

懵懂的女人 提交于 2020-01-03 06:59:06
问题 I have a base class that basically wraps up attaching a class to a arbitrary windows handle (e.g, HWND, HFONT), and uses a policy class to attach/detach and destroy: // class SmartHandle template<typename THANDLE, class TWRAPPER, class TPOLICY> class SmartHandle : boost::noncopyable { private: TPOLICY* m_pPolicy; // Policy bool m_bIsTemporary; // Is this a temporary window? SmartHandle(); // no default ctor SmartHandle(const SmartHandle<THANDLE, TWRAPPER, TPOLICY>&); // no cctor protected:

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

给你一囗甜甜゛ 提交于 2019-12-31 20:17:09
问题 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

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

泄露秘密 提交于 2019-12-31 20:16:13
问题 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

Move constructors and inheritance

送分小仙女□ 提交于 2019-12-30 05:54:11
问题 I am trying to understand the way move constructors and assignment ops work in C++11 but I'm having problems with delegating to parent classes. The code: class T0 { public: T0() { puts("ctor 0"); } ~T0() { puts("dtor 0"); } T0(T0 const&) { puts("copy 0"); } T0(T0&&) { puts("move 0"); } T0& operator=(T0 const&) { puts("assign 0"); return *this; } T0& operator=(T0&&) { puts("move assign 0"); return *this; } }; class T : public T0 { public: T(): T0() { puts("ctor"); } ~T() { puts("dtor"); } T(T