move-semantics

move Constructor is not called

自闭症网瘾萝莉.ら 提交于 2020-01-23 13: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)

C++ move constructor not called for rvalue reference [duplicate]

笑着哭i 提交于 2020-01-23 12:26:46
问题 This question already has answers here : On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule (3 answers) Closed 3 years ago . class MyClass { public: MyClass() { std::cout << "default constructor\n"; } MyClass(MyClass& a) { std::cout << "copy constructor\n"; } MyClass(MyClass&& b) { std::cout << "move constructor\n"; } }; void test(MyClass&& temp) { MyClass a(MyClass{}); // calls MOVE constructor as expected MyClass b(temp); // calls COPY constructor... not expected...? }

Does std::vector.push_back(std::move(foo)) make sense?

谁都会走 提交于 2020-01-23 08:27:53
问题 I have come across this in some code (details eliminated for clarity): std::vector<std::vector<int>> foo; { std::vector<int> bar = {42}; foo.push_back(std::move(bar)); // Hmmm... } // Indicate `bar` is no longer needed. The std::move looks unnecessary to me, but is it? Is the behaviour any different from just foo.push_back(bar); ? What if instead of an int the element is a class such as pcl::PointXYZ as it is in my actual code? UPDATE : I have altered the code to more explicitly indicate that

Does std::vector.push_back(std::move(foo)) make sense?

柔情痞子 提交于 2020-01-23 08:27:24
问题 I have come across this in some code (details eliminated for clarity): std::vector<std::vector<int>> foo; { std::vector<int> bar = {42}; foo.push_back(std::move(bar)); // Hmmm... } // Indicate `bar` is no longer needed. The std::move looks unnecessary to me, but is it? Is the behaviour any different from just foo.push_back(bar); ? What if instead of an int the element is a class such as pcl::PointXYZ as it is in my actual code? UPDATE : I have altered the code to more explicitly indicate that

Swapping with rvalues

我们两清 提交于 2020-01-22 13:37:28
问题 Suppose I want swap that works on rvalues, and don't want to write 4 versions for all combinations of rvalue/lvalue references (rvalue/rvalue version is kinda pointless but it doesn't hurt). I came up with this: template <typename A, typename B> struct is_same_no_ref : std::is_same< typename std::remove_reference<A>::type, typename std::remove_reference<B>::type > {}; template <typename A, typename B, typename = typename std::enable_if<is_same_no_ref<A, B>::value>::type > inline void my_swap

Swapping with rvalues

北城余情 提交于 2020-01-22 13:37:05
问题 Suppose I want swap that works on rvalues, and don't want to write 4 versions for all combinations of rvalue/lvalue references (rvalue/rvalue version is kinda pointless but it doesn't hurt). I came up with this: template <typename A, typename B> struct is_same_no_ref : std::is_same< typename std::remove_reference<A>::type, typename std::remove_reference<B>::type > {}; template <typename A, typename B, typename = typename std::enable_if<is_same_no_ref<A, B>::value>::type > inline void my_swap

Returning std::vector by value

最后都变了- 提交于 2020-01-20 02:17:31
问题 It is often said that in C++11 it is sane to return std::vector by value. In C++03 this was mostly true as RVO should optimize away the copy. But that should scared most developers away. In C++11 will a returned std::vector local variable always be moved? What if that vector is a member of a local variable instead of a local variable itself? Obviously returning a global variable will not be moved. What other cases will it not be moved? 回答1: First, every time a copy could be elided before, it

Returning std::vector by value

半世苍凉 提交于 2020-01-20 02:17:31
问题 It is often said that in C++11 it is sane to return std::vector by value. In C++03 this was mostly true as RVO should optimize away the copy. But that should scared most developers away. In C++11 will a returned std::vector local variable always be moved? What if that vector is a member of a local variable instead of a local variable itself? Obviously returning a global variable will not be moved. What other cases will it not be moved? 回答1: First, every time a copy could be elided before, it

Cannot move std::any

旧街凉风 提交于 2020-01-16 18:02:06
问题 The following code using vptr = std::vector<std::unique_ptr<int>>; auto m = std::unordered_map<int, std::any>{}; m.try_emplace(0, move(vptr{})); Fails to compile, complaining about using of deleted copy constructor of unique_ptr . After replacing std::any with vptr in template argument this code compiles, so the issue is clearly with any How can I force std::any to be moved instead of copied? 回答1: The problem is not moving std::any, it's that std::any itself does not support move-only types

Cannot move std::any

坚强是说给别人听的谎言 提交于 2020-01-16 18:01:09
问题 The following code using vptr = std::vector<std::unique_ptr<int>>; auto m = std::unordered_map<int, std::any>{}; m.try_emplace(0, move(vptr{})); Fails to compile, complaining about using of deleted copy constructor of unique_ptr . After replacing std::any with vptr in template argument this code compiles, so the issue is clearly with any How can I force std::any to be moved instead of copied? 回答1: The problem is not moving std::any, it's that std::any itself does not support move-only types