move-constructor

Why is this copy constructor called rather than the move constructor?

假装没事ソ 提交于 2019-11-28 12:11:14
The following code snippet causes the copy constructor to be called where I expected the move constructor to be called: #include <cstdio> struct Foo { Foo() { puts("Foo gets built!"); } Foo(const Foo& foo) { puts("Foo gets copied!"); } Foo(Foo&& foo) { puts("Foo gets moved!"); } }; struct Bar { Foo foo; }; Bar Meow() { Bar bar; return bar; } int main() { Bar bar(Meow()); } On VS11 Beta, in debug mode, this prints: Foo gets built! Foo gets copied! Foo gets copied! I checked the standard and Bar seems to meet all requirements to have a default move constructor automatically generated, yet that

Understanding `std::is_move_constructible`

橙三吉。 提交于 2019-11-28 10:55:02
Types without a move constructor, but with a copy constructor that accepts const T& arguments, satisfy std::is_move_constructible . For example, in the following code: #include <type_traits> struct T { T(const T&) {} //T(T&&) = delete; }; int main() { static_assert(std::is_move_constructible<T>::value, "not move constructible"); return 0; } T will have no implicit move constructor as it has a user-defined copy constructor. However, if we uncomment the explicit delete of the move constructor, the code no longer compiles. Why is this? I would have expected that the explicit copy constructor

Move Constructor vs Copy Elision. Which one gets called?

我的未来我决定 提交于 2019-11-28 10:25:20
I have two pieces of code here to show you. They are two classes and each one provides a Move Constructor and a function which returns a temporary. In the first case, the function returning a temporary calls the Move Constructor In the second case, the function returning a temporary just tells the compiler to perform a copy elision I'm confused: in both cases I define a Move Constructor and a random member function returning a temporary. But the behavior changes, and my question is why . Note that in the following examples, the operator<< was overloaded in order to print a list (in the first

Move out element of std priority_queue in C++11

雨燕双飞 提交于 2019-11-27 23:58:29
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 moved = std::move(l.top()); // error from the above line: // use of deleted function ‘MyClass::MyClass

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

岁酱吖の 提交于 2019-11-27 13:50:54
问题 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

When Does Move Constructor get called?

£可爱£侵袭症+ 提交于 2019-11-27 11:15:25
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(noConstruct); a(const a&); a& operator=(const a&); a(a&&); a& operator=(a&&); ~a(); }; a::a() { Array=new

Why is this copy constructor called rather than the move constructor?

有些话、适合烂在心里 提交于 2019-11-27 06:51:02
问题 The following code snippet causes the copy constructor to be called where I expected the move constructor to be called: #include <cstdio> struct Foo { Foo() { puts("Foo gets built!"); } Foo(const Foo& foo) { puts("Foo gets copied!"); } Foo(Foo&& foo) { puts("Foo gets moved!"); } }; struct Bar { Foo foo; }; Bar Meow() { Bar bar; return bar; } int main() { Bar bar(Meow()); } On VS11 Beta, in debug mode, this prints: Foo gets built! Foo gets copied! Foo gets copied! I checked the standard and

Understanding `std::is_move_constructible`

ぃ、小莉子 提交于 2019-11-27 05:59:11
问题 Types without a move constructor, but with a copy constructor that accepts const T& arguments, satisfy std::is_move_constructible . For example, in the following code: #include <type_traits> struct T { T(const T&) {} //T(T&&) = delete; }; int main() { static_assert(std::is_move_constructible<T>::value, "not move constructible"); return 0; } T will have no implicit move constructor as it has a user-defined copy constructor. However, if we uncomment the explicit delete of the move constructor,

Move Constructor vs Copy Elision. Which one gets called?

爱⌒轻易说出口 提交于 2019-11-27 03:35:26
问题 I have two pieces of code here to show you. They are two classes and each one provides a Move Constructor and a function which returns a temporary. In the first case, the function returning a temporary calls the Move Constructor In the second case, the function returning a temporary just tells the compiler to perform a copy elision I'm confused: in both cases I define a Move Constructor and a random member function returning a temporary. But the behavior changes, and my question is why . Note

Are move constructors produced automatically?

你说的曾经没有我的故事 提交于 2019-11-27 01:19:01
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? 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 constructor would not be implicitly defined as deleted. So for example, if your class has a class type data member