move-semantics

Prevent moving of a unique_ptr C++11

女生的网名这么多〃 提交于 2020-01-04 13:42:21
问题 Is there any way to prevent a user to explicity take ownership of a unique pointer with std::move ? 回答1: Make it const The unique_ptr move constructor takes a non-const rvalue reference, so can't be called with a const object. const unique_ptr<int> owner(new int); // ... unique_ptr<int> thief = std::move(owner); // ERROR This allows unique_ptr to be used like a boost::scoped_ptr 回答2: By returning a std::unique_ptr , you have given up the control of the object. The new owner will either

Forward individual members of a Forward reference

拜拜、爱过 提交于 2020-01-03 18:52:50
问题 I have a function that potentially moves a generic argument but through their members. What of these options is more correct: This seems the more natural but it is strange because the argument is potentially moved twice [a], which is odd because the object can become invalid. template<class T> void fun(T&& t){ myhead_ = std::forward<T>(t).head_; myrest_ = std::forward<T>(t).rest_; } This can't be incorrect but it may not be moving anything. template<class T> void fun(T&& t){ myhead_ = std:

Forward individual members of a Forward reference

萝らか妹 提交于 2020-01-03 18:52:31
问题 I have a function that potentially moves a generic argument but through their members. What of these options is more correct: This seems the more natural but it is strange because the argument is potentially moved twice [a], which is odd because the object can become invalid. template<class T> void fun(T&& t){ myhead_ = std::forward<T>(t).head_; myrest_ = std::forward<T>(t).rest_; } This can't be incorrect but it may not be moving anything. template<class T> void fun(T&& t){ myhead_ = std:

Is it possible to take memory from std::string(like string move ctor does)?

岁酱吖の 提交于 2020-01-03 09:16:17
问题 If I have my internal class that is my own version of vector<char> (I control the source) and for the sake of example I can not change it to be std::string is there a way to steal memory from std::string , just like move constructor of std::string does. So something like this: std::string str{"abcdefghijklmnopqrstu"}; MyVectorCharClass mvc(std::move(str)); // Constructor takes memory from str I think I heard of some future proposals to add .release() to std::string or std::vector but I am

C++11 - emplace_back between 2 vectors doesn't work

泪湿孤枕 提交于 2020-01-03 08:39:25
问题 I was trying to adapt some code and moving the content from a vector to another one using emplace_back() #include <iostream> #include <vector> struct obj { std::string name; obj():name("NO_NAME"){} obj(const std::string& _name):name(_name){} obj(obj&& tmp): name(std::move(tmp.name)) {} obj& operator=(obj&& tmp) = default; }; int main(int argc, char* argv[]) { std::vector<obj> v; for( int i = 0; i < 1000; ++i ) { v.emplace_back(obj("Jon")); } std::vector<obj> p; for( int i = 0; i < 1000; ++i )

C++11 - emplace_back between 2 vectors doesn't work

不问归期 提交于 2020-01-03 08:39:05
问题 I was trying to adapt some code and moving the content from a vector to another one using emplace_back() #include <iostream> #include <vector> struct obj { std::string name; obj():name("NO_NAME"){} obj(const std::string& _name):name(_name){} obj(obj&& tmp): name(std::move(tmp.name)) {} obj& operator=(obj&& tmp) = default; }; int main(int argc, char* argv[]) { std::vector<obj> v; for( int i = 0; i < 1000; ++i ) { v.emplace_back(obj("Jon")); } std::vector<obj> p; for( int i = 0; i < 1000; ++i )

C++11: std::move() call on arguments' list

家住魔仙堡 提交于 2020-01-02 09:01:14
问题 Is it safe to operate on object within arguments' list, when there is also std::move() invoked on that object ? void foo(int* raw, std::unique_ptr<int> u) { *raw = 456; } std::unique_ptr<int> p(new int(123)); foo(p.get(), std::move(p)); Will the `raw' pointer in foo() be valid if std::move(p) was evaluated as the first parameter ? 回答1: No, it's NOT safe. the eval order of argument is not specified in standard. So your code can be run as: std::move(p) . call move constructor of std::unique_ptr

Is move assignment via destruct+move construct safe?

有些话、适合烂在心里 提交于 2020-01-02 01:55:11
问题 Here's a very easy way to define move assignment for most any class with a move constructor: class Foo { public: Foo(Foo&& foo); // you still have to write this one Foo& operator=(Foo&& foo) { if (this != &foo) { // avoid destructing the only copy this->~Foo(); // call your own destructor new (this) Foo(std::move(foo)); // call move constructor via placement new } return *this; } // ... }; Is this sequence of calling your own destructor followed by placement new on the this pointer safe in

Why would const-ness of a local variable inhibit move semantics for the returned value?

人盡茶涼 提交于 2020-01-02 00:41:31
问题 struct STest : public boost::noncopyable { STest(STest && test) : m_n( std::move(test.m_n) ) {} explicit STest(int n) : m_n(n) {} int m_n; }; STest FuncUsingConst(int n) { STest const a(n); return a; } STest FuncWithoutConst(int n) { STest a(n); return a; } void Caller() { // 1. compiles just fine and uses move ctor STest s1( FuncWithoutConst(17) ); // 2. does not compile (cannot use move ctor, tries to use copy ctor) STest s2( FuncUsingConst(17) ); } The above example illustrates how in C+

Pass by value and move, or two methods [duplicate]

安稳与你 提交于 2020-01-01 11:58:11
问题 This question already has answers here : Why is value taking setter member functions not recommended in Herb Sutter's CppCon 2014 talk (Back to Basics: Modern C++ Style)? (4 answers) Closed 4 years ago . Assume I have the following class, which has a method set_value . Which implementation is better? class S { public: // a set_value method private: Some_type value; }; Pass by value, then move void S::set_value(Some_type value) { this->value = std::move(value); } Define two overloaded methods