move-constructor

Inheriting copy and move constructors of base class using “using” keyword

戏子无情 提交于 2019-11-30 09:33:55
问题 I want to inherit copy constructor of the base class using using keyword: #include <iostream> struct A { A() = default; A(const A &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; } A( A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; } A& operator=(const A &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; } A& operator=( A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; } }; struct B : A { using A::A; using A::operator=; B& operator=(const B &) { std

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

。_饼干妹妹 提交于 2019-11-30 01:20:18
问题 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

move constructor not being called as expected

早过忘川 提交于 2019-11-29 17:16:28
问题 I'm new to C++0x and I'm trying to wrap my head around rvalue references and move constructors. I'm using g++ 4.4.6 with -std=c++0x, and I'm confused by the following piece of code: class Foo { public: Foo() : p( new int(0) ) { printf("default ctor\n"); } Foo( int i ) : p( new int(i) ) { printf("int ctor\n"); } ~Foo() { delete p; printf("destructor\n"); } Foo( const Foo& other ) : p( new int( other.value() ) ) { printf("copy ctor\n"); } Foo( Foo&& other ) : p( other.p ) { printf("move ctor\n"

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

最后都变了- 提交于 2019-11-29 17:14:16
问题 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

Inheriting copy and move constructors of base class using “using” keyword

ぐ巨炮叔叔 提交于 2019-11-29 16:33:32
I want to inherit copy constructor of the base class using using keyword: #include <iostream> struct A { A() = default; A(const A &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; } A( A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; } A& operator=(const A &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; } A& operator=( A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; } }; struct B : A { using A::A; using A::operator=; B& operator=(const B &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; } B& operator=( B &&) { std::cerr << __PRETTY

Move constructor signature

旧城冷巷雨未停 提交于 2019-11-29 10:30:31
From this reference, it allows a const rvalue as a move constructor Type::Type( const Type&& other ); How can a movable object be const ? Even if this was technically allowed, is there a case where such declaration would be useful? Jonathan Wakely How can a movable object be const ? It can't, but that's not what the language says. The language says that a constructor with that signature is a "move constructor" but that doesn't mean the argument gets moved from, it just means the constructor meets the requirements of a "move constructor". A move constructor is not required to move anything, and

Why doesn't C++ move construct rvalue references by default? [duplicate]

不羁的心 提交于 2019-11-29 09:29:33
This question already has an answer here: Rvalue Reference is Treated as an Lvalue? 4 answers Lvalue reference constructor is called instead of rvalue reference constructor 1 answer Say I have the following function void doWork(Widget && param) // param is an LVALUE of RRef type { Widget store = std::move(param); } Why do I need to cast param back to an rvalue with std::move() ? Shouldn't it be obvious that the type of param is rvalue since it was declared in the function signature as an rvalue reference? Shouldn't the move constructor be automatically invoked here on this principle alone? Why

Does Visual Studio 2017 need an explicit move constructor declaration?

╄→гoц情女王★ 提交于 2019-11-28 23:27:33
The below code can be compiled successfully using Visual Studio 2015, but it failed using Visual Studio 2017. Visual Studio 2017 reports: error C2280: “std::pair::pair(const std::pair &)”: attempting to reference a deleted function Code #include <unordered_map> #include <memory> struct Node { std::unordered_map<int, std::unique_ptr<int>> map_; // Uncommenting the following two lines will pass Visual Studio 2017 compilation //Node(Node&& o) = default; //Node() = default; }; int main() { std::vector<Node> vec; Node node; vec.push_back(std::move(node)); return 0; } It looks like Visual Studio

What is copy/move constructor choosing rule in C++? When does move-to-copy fallback happen?

筅森魡賤 提交于 2019-11-28 21:26:51
The first example: #include <iostream> #include <memory> using namespace std; struct A { unique_ptr<int> ref; A(const A&) = delete; A(A&&) = default; A(const int i) : ref(new int(i)) { } ~A() = default; }; int main() { A a[2] = { 0, 1 }; return 0; } It works perfectly. So here the MOVE constructor is used. Let's remove the move constructor and add a copy one: #include <iostream> #include <memory> using namespace std; struct A { unique_ptr<int> ref; A(const A&a) : ref( a.ref.get() ? new int(*a.ref) : nullptr ) { } A(A&&) = delete; A(const int i) : ref(new int(i)) { } ~A() = default; }; int main

C++11 rvalue reference calling copy constructor too

旧巷老猫 提交于 2019-11-28 21:13:31
I've been testing some C++11 features from some some. I came across r-value references and move constructors. I implemented my first move constructor, here it is: #include <iostream> #include <vector> using namespace std; class TestClass{ public: TestClass(int s): size(s), arr(new int[s]){ } ~TestClass(){ if (arr) delete arr; } // copy constructor TestClass(const TestClass& other): size(other.size), arr(new int[other.size]){ std::copy(other.arr, other.arr + other.size, arr); } // move constructor TestClass(TestClass&& other){ arr=other.arr; size=other.size; other.arr=nullptr; other.size=0; }