move-constructor

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

Move constructor is not called when throwing an exception

故事扮演 提交于 2019-12-02 05:08:07
问题 I have a variable which accumulates the current exception and needs to get cleaned when the current exception gets thrown (so that the same error does not get reported again). The problem is that throw std::move(ex); does not call the move constructor (which would clean ex ), but rather calls a copy constructor (so that ex stays with the already thrown errors too). A MVCE follows: #include <iostream> #include <stdexcept> #include <string> using namespace std; class ThrowMoveTest : exception {

move constructor and copy constructor in C++

吃可爱长大的小学妹 提交于 2019-12-01 09:27:55
My understanding is that a move constructor is called if it exists when we return a local object from a function. However, I ran into a situation where the copy constructor was called instead, as shown in the following example in function foo2() . Why did that happen? #include <cstdio> #include <memory> #include <thread> #include <chrono> class tNode { public: tNode(int b = 10) { a = b; printf("a: %d, default constructor %s() is called at %s:%d \n", a, __func__, __FILE__, __LINE__); } tNode(const tNode& node) { a = node.a; printf("a: %d, copy constructor %s() is called at %s:%d \n", a, __func_

Use std::move in C++11 move constructor with uniform initialization syntax

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 09:17:15
I have this simple class: struct Worker { Worker() : done{false} {} Worker(const Worker& rhs) : done{rhs.done}, qworker{} {} Worker(Worker &&rhs) : done{rhs.done} { qworker = std::move(rhs.qworker); } ... } this compile fine with gcc-4.7.2 but if I try to use this version I obtain an error struct Worker { Worker() : done{false} {} Worker(const Worker& rhs) : done{rhs.done}, qworker{} {} Worker(Worker &&rhs) : done{rhs.done} , qworker{std::move(rhs.qworker)} // <- ERROR { } ... } Why? In file included from tlog.cpp:8:0: log11.hpp: In member function ‘void Log11::Worker::run()’: log11.hpp:34:29:

move constructor and copy constructor in C++

荒凉一梦 提交于 2019-12-01 06:49:10
问题 My understanding is that a move constructor is called if it exists when we return a local object from a function. However, I ran into a situation where the copy constructor was called instead, as shown in the following example in function foo2() . Why did that happen? #include <cstdio> #include <memory> #include <thread> #include <chrono> class tNode { public: tNode(int b = 10) { a = b; printf("a: %d, default constructor %s() is called at %s:%d \n", a, __func__, __FILE__, __LINE__); } tNode

Move constructors and inheritance

▼魔方 西西 提交于 2019-11-30 17:28:16
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 const& o): T0(o) { puts("copy"); } T(T&& o): T0(o) { puts("move"); } T& operator=(T const& o) { puts(

Why does a move constructor require a default constructor for its members?

六月ゝ 毕业季﹏ 提交于 2019-11-30 17:13:51
I was trying to implement a move constructor for a class without a copy constructor. I got an error that the default constructor for a member of the class was missing. Here's a trivial example to illustrate this: struct A { public: A() = delete; A(A const&) = delete; A(A &&a) {} }; struct B { A a; B() = delete; B(B const&) = delete; B(B &&b) {} }; Trying to compile this, I get: move_without_default.cc: In constructor ‘B::B(B&&)’: move_without_default.cc:15:11: error: use of deleted function ‘A::A()’ B(B &&b) {} ^ move_without_default.cc:6:2: note: declared here A() = delete; ^ Why is this an

Move constructor and const member variables

大城市里の小女人 提交于 2019-11-30 14:31:03
问题 I like the idea of const member variables especially when I wrap C functions into classes. The constructor takes a resource handle (e.g. a file descriptor) that stays valid during the whole object life time and the destructor finally closes it. (That is the idea behind RAII, right?) But with the C++0x move constructor i run into a problem. Since the destructor is also called on the "unloaded" object i need to prevent the cleanup of the resource handle. Since the member variable is const i

Why do I need to use std::move in the initialization list of a move-constructor?

本小妞迷上赌 提交于 2019-11-30 11:46:53
Let's say I have a (trivial) class, which is move-constructible and move-assignable but not copy-constructable or copy-assignable: class movable { public: explicit movable(int) {} movable(movable&&) {} movable& operator=(movable&&) { return *this; } movable(const movable&) = delete; movable& operator=(const movable&) = delete; }; This works fine: movable m1(movable(17)); This, of course, does not work, because m1 is not an rvalue: movable m2(m1); But, I can wrap m1 in std::move , which casts it to an rvalue-reference, to make it work: movable m2(std::move(m1)); So far, so good. Now, let's say

Move constructor and const member variables

泄露秘密 提交于 2019-11-30 10:55:14
I like the idea of const member variables especially when I wrap C functions into classes. The constructor takes a resource handle (e.g. a file descriptor) that stays valid during the whole object life time and the destructor finally closes it. (That is the idea behind RAII, right?) But with the C++0x move constructor i run into a problem. Since the destructor is also called on the "unloaded" object i need to prevent the cleanup of the resource handle. Since the member variable is const i have no way to assign the value -1 or INVALID_HANDLE (or equivalent values) to indicate to the destructor