move-constructor

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

徘徊边缘 提交于 2019-11-26 22:25:31
问题 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

Move out element of std priority_queue in C++11

北战南征 提交于 2019-11-26 21:37:13
问题 Minimal working example. #include <cassert> #include <list> #include <queue> //#define USE_PQ struct MyClass { const char* str; MyClass(const char* _str) : str(_str) {} MyClass(MyClass&& src) { str = src.str; src.str = nullptr; } MyClass(const MyClass&) = delete; }; struct cmp_func { bool operator() (const MyClass&, const MyClass&) const { return true; } }; typedef std::priority_queue<MyClass, std::vector<MyClass>, cmp_func> pq_type; #ifdef USE_PQ MyClass remove_front(pq_type& l) { MyClass

Should a move constructor take a const or non-const rvalue reference?

那年仲夏 提交于 2019-11-26 19:51:12
问题 In several places I've seen the recommended signatures of copy and move constructors given as: struct T { T(); T(const T& other); T(T&& other); }; Where the copy constructor takes a const reference, and the move constructor takes a non-const rvalue reference. As far as I can see though, this prevents me taking advantage of move semantics when returning const objects from a function, such as in the case below: T generate_t() { const T t; return t; } Testing this with VC11 Beta, T 's copy

Why is this code trying to call the copy constructor?

江枫思渺然 提交于 2019-11-26 19:06:20
I just spent an inordinate amount of time fiddling with a complilation error in Visual Studio. I have distilled the code into the small compilable example below and tried it on IdeOne and got the same error which you can see here . I am wondering why the following code tries to call B(const B&) instead of B(B&&) : #include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a noncopyable class A(const A&); A& operator=(const A&); }; class B : public A { }; int main() { B binst;

When Does Move Constructor get called?

余生长醉 提交于 2019-11-26 15:26:42
问题 I'm confused about when a move constructor gets called vs a copy constructor. I've read the following sources: Move constructor is not getting called in C++0x Move semantics and rvalue references in C++11 msdn All of these sources are either overcomplicated(I just want a simple example) or only show how to write a move constructor, but not how to call it. Ive written a simple problem to be more specific: const class noConstruct{}NoConstruct; class a { private: int *Array; public: a(); a

How should I deal with mutexes in movable types in C++?

一世执手 提交于 2019-11-26 12:51:13
By design, std::mutex is not movable nor copy-constructable. This means that a class A , which holds a mutex, won't receive a default-move-constructor. How would I make this type A movable in a thread-safe way? Let's start with a bit of code: class A { using MutexType = std::mutex; using ReadLock = std::unique_lock<MutexType>; using WriteLock = std::unique_lock<MutexType>; mutable MutexType mut_; std::string field1_; std::string field2_; public: ... I've put some rather suggestive type aliases in there that we won't really take advantage of in C++11, but become much more useful in C++14. Be

Are move constructors produced automatically?

对着背影说爱祢 提交于 2019-11-26 09:36:48
问题 I have a big class holding a lot of STL containers. Will the compiler automatically make a move constructor that will move those containers to the target or I have to make my own? 回答1: A move constructor for a class X is implicitly declared as defaulted exactly when 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 destructor, and the move

Why is this code trying to call the copy constructor?

南笙酒味 提交于 2019-11-26 06:48:01
问题 I just spent an inordinate amount of time fiddling with a complilation error in Visual Studio. I have distilled the code into the small compilable example below and tried it on IdeOne and got the same error which you can see here. I am wondering why the following code tries to call B(const B&) instead of B(B&&) : #include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a

How should I deal with mutexes in movable types in C++?

限于喜欢 提交于 2019-11-26 03:07:15
问题 By design, std::mutex is not movable nor copy-constructable. This means that a class A , which holds a mutex, won\'t receive a default-move-constructor. How would I make this type A movable in a thread-safe way? 回答1: Let's start with a bit of code: class A { using MutexType = std::mutex; using ReadLock = std::unique_lock<MutexType>; using WriteLock = std::unique_lock<MutexType>; mutable MutexType mut_; std::string field1_; std::string field2_; public: ... I've put some rather suggestive type

Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?

让人想犯罪 __ 提交于 2019-11-25 22:36:34
问题 I want to refresh my memory on the conditions under which a compiler typically auto generates a default constructor, copy constructor and assignment operator. I recollect there were some rules, but I don\'t remember, and also can\'t find a reputable resource online. Can anyone help? 回答1: In the following, "auto-generated" means "implicitly declared as defaulted, but not defined as deleted". There are situations where the special member functions are declared, but defined as deleted. The