move-semantics

How to move elements out of STL priority queue

﹥>﹥吖頭↗ 提交于 2019-12-19 05:47:58
问题 C++'s STL priority queue have a void pop() method, and a const ref top() method. Thus, if you want to move elements out of the queue, you have to do something like this: T moved = std::move(const_cast<T&>(myQueue.top()))); myQeue.pop(); This effectively casts the top to not a constant, so that it can be moved (rather than copied). I don't like this code, because the forced move may invalidate the invariants of the priority queue, which should not matter because of the pop, but things could go

Is it possible to move a boost::optional?

核能气质少年 提交于 2019-12-19 05:02:59
问题 I've been trying to define a defaulted move constructor in a class with a boost::optional member variable. #include <boost/optional.hpp> #include <utility> #include <vector> struct bar {std::vector<int> vec;}; struct foo { foo() = default; foo(foo&&) = default; boost::optional<bar> hello; }; int main() { foo a; foo b(std::move(a)); } My compiler supports both move semantics and defaulted move constructors, but I cannot get this to work. % clang++ foo.cc -std=c++11 -stdlib=libc++ foo.cc:15:7:

When are lvalues moved instead of copied in C++?

孤者浪人 提交于 2019-12-19 02:13:56
问题 Given the following: Foo getFoo() { Foo result = doSomeWork(); return result; } Does C++ guarantee that result will be moved, instead of copied? Or to put it another way, is writing return std::move(result) superfluous? Are there any (other) situations where the standard specifies that a lvalue will be silently moved instead of copied, in the absence of an explicit std::move cast? Notes: Assume Foo is move-constructible. Disregarding copy/move elision, which may apply in addition. 回答1:

When are lvalues moved instead of copied in C++?

房东的猫 提交于 2019-12-19 02:11:10
问题 Given the following: Foo getFoo() { Foo result = doSomeWork(); return result; } Does C++ guarantee that result will be moved, instead of copied? Or to put it another way, is writing return std::move(result) superfluous? Are there any (other) situations where the standard specifies that a lvalue will be silently moved instead of copied, in the absence of an explicit std::move cast? Notes: Assume Foo is move-constructible. Disregarding copy/move elision, which may apply in addition. 回答1:

When are lvalues moved instead of copied in C++?

北城余情 提交于 2019-12-19 02:10:20
问题 Given the following: Foo getFoo() { Foo result = doSomeWork(); return result; } Does C++ guarantee that result will be moved, instead of copied? Or to put it another way, is writing return std::move(result) superfluous? Are there any (other) situations where the standard specifies that a lvalue will be silently moved instead of copied, in the absence of an explicit std::move cast? Notes: Assume Foo is move-constructible. Disregarding copy/move elision, which may apply in addition. 回答1:

When is the move constructor called in the `std::move()` function?

允我心安 提交于 2019-12-18 19:12:21
问题 The function std::move() is defined as template<typename T> typename std::remove_reference<T>::type&& move(T && t) { return static_cast<typename std::remove_reference<T>::type&&>( t ); } There are four places where I can imagine the move constructor to be called: When the parameter is passed. When the cast is performed. When the result is returned. Not in the std::move() function itself but possibly at the place where the returned reference ultimately arrives. I would bet for number 4, but I

When is the move constructor called in the `std::move()` function?

ⅰ亾dé卋堺 提交于 2019-12-18 19:12:07
问题 The function std::move() is defined as template<typename T> typename std::remove_reference<T>::type&& move(T && t) { return static_cast<typename std::remove_reference<T>::type&&>( t ); } There are four places where I can imagine the move constructor to be called: When the parameter is passed. When the cast is performed. When the result is returned. Not in the std::move() function itself but possibly at the place where the returned reference ultimately arrives. I would bet for number 4, but I

Private constructor inhibits use of emplace[_back]() to avoid a move

醉酒当歌 提交于 2019-12-18 18:59:22
问题 Consider the following code: #include <vector> class A { public: A(A&&); // somewhat expensive static std::vector<A> make_As() { std::vector<A> result; result.push_back(A(3)); result.push_back(A(4)); return result; } private: A(int); // private constructor }; Since A 's move constructor is somewhat expensive (for whatever reason), I'd like to avoid calling it and use emplace_back() instead: #include <vector> class A { public: A(A&&); // somewhat expensive static std::vector<A> make_As() { std

Are move semantics incomplete?

家住魔仙堡 提交于 2019-12-18 15:48:16
问题 Move semantics replace copy semantics in situations where copying is inefficient. Copy semantics deals fully with copyable objects, including const objects. Already, there exists a myriad of non-copyable objects in c++11, for example std::unique_ptr. These objects rely on move semantics completely because moving from an object allows for invalidating it. This is important (imho) for popular design patterns like RAII. A problem occurs when a const non-copyable object is assigned to an area of

Are move semantics incomplete?

≯℡__Kan透↙ 提交于 2019-12-18 15:48:00
问题 Move semantics replace copy semantics in situations where copying is inefficient. Copy semantics deals fully with copyable objects, including const objects. Already, there exists a myriad of non-copyable objects in c++11, for example std::unique_ptr. These objects rely on move semantics completely because moving from an object allows for invalidating it. This is important (imho) for popular design patterns like RAII. A problem occurs when a const non-copyable object is assigned to an area of