move-constructor

Move constructor signature

最后都变了- 提交于 2019-12-18 06:09:09
问题 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? 回答1: 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

Does Visual Studio 2017 need an explicit move constructor declaration?

為{幸葍}努か 提交于 2019-12-18 02:30:09
问题 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::push_back insists on using copy constructor though a move constructor is provided

情到浓时终转凉″ 提交于 2019-12-18 02:16:08
问题 I was receiving a strange error from gcc and cannot figure out why. I made the following example code to make the problem more clear. Basically, there is a class defined, for which I make its copy constructor and copy assignment operator private, to prevent calling them accidentally. #include <vector> #include <cstdio> using std::vector; class branch { public: int th; private: branch( const branch& other ); const branch& operator=( const branch& other ); public: branch() : th(0) {} branch(

vector::push_back insists on using copy constructor though a move constructor is provided

只谈情不闲聊 提交于 2019-12-18 02:16:01
问题 I was receiving a strange error from gcc and cannot figure out why. I made the following example code to make the problem more clear. Basically, there is a class defined, for which I make its copy constructor and copy assignment operator private, to prevent calling them accidentally. #include <vector> #include <cstdio> using std::vector; class branch { public: int th; private: branch( const branch& other ); const branch& operator=( const branch& other ); public: branch() : th(0) {} branch(

C++11 rvalue reference calling copy constructor too

断了今生、忘了曾经 提交于 2019-12-17 22:58:37
问题 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

move-construct object with placement new

我的未来我决定 提交于 2019-12-12 19:45:25
问题 Is it not UB to move-construct an object via placement new? Let's say I have this code: class Foo { public: Foo() { foo_ = new int; } ~Foo() { delete foo_; } Foo(Foo &&f) { foo_ = std::swap(f.foo_, foo_); } private: int* foo_; } void bar() { void* pMem = malloc(sizeof(Foo)); Foo f1; // move-construct with placement new: new((Foo*)pMem) Foo(std::move(f1)); // f2 in *pMem // now f1 will contain a pointer foo_ of undefined value // when exiting scope f1.~Foo(){} will exhibit UB trying to delete

Copy or Move Constructor for a class with a member std::mutex (or other non-copyable object)?

橙三吉。 提交于 2019-12-10 19:57:28
问题 class A { private: class B { private: std::mutex mu; A* parent = NULL; public: B(A* const parent_ptr): parent(parent_ptr) {} B(const A::B & b_copy) { /* I thought I needed code here */ } }; public: B b = B(this); //...to make this copy instruction work. // (Copy constructor is deleted, need to declare a new one?) }; I have a class B that is basically a thread-safe task queue. It contains a deque , a mutex , and a condition_variable . It facilitates a consumer/producer relationship between any

Disallow the use of auto for a given class, C++14 vs. C++17 update

谁说我不能喝 提交于 2019-12-06 15:31:25
问题 What is the feature that allows me use auto for non-copyable (and non-movable) types in C++17 and didn't for C++14? Consider the following code: struct A{ A(A const&)=delete; A(A&&)=delete; }; int main(){ auto a1 = A{}; // ok in C++17, not ok in C++14 auto&& a2 = A{}; // ok in C++17, ok in C++14 } It turns out that this was invalid code in C++14 but it is valid in C++17. The behavior is consistent in clang and gcc: https://godbolt.org/z/af8mEc The reason I ask is because until recently I was

move Constructor is not called

萝らか妹 提交于 2019-12-06 04: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) :data(new int[size]), count(size), capacity(size) {std::cout << "Constructor called with size: " << size

Explicit move constructor

核能气质少年 提交于 2019-12-05 17:22:38
问题 Trying to compile the following code: struct Foo { explicit Foo ( void ) { } explicit Foo ( Foo&& rhs ) { } }; Foo bar ( void ) { return Foo(); } Getting the following error: call to implicitly-deleted copy constructor of 'Foo' Well, it's quite obvious that the copy-ctor is implicitly deleted. Question 1: Why does the compiler need the copy-ctor of Foo ? I expected the return value of bar to be constructed from the rvalue Foo() with move-ctor. Then I redeclare the move-ctor as implicit and