move-semantics

Is std::move(*this) a good pattern?

别说谁变了你拦得住时间么 提交于 2019-12-20 11:07:09
问题 In order to make this code with C++11 reference qualifiers work as expected I have to introduce a std::move(*this) that doesn't sound right. #include<iostream> struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun() &&{std::cout << "gun&&" << std::endl;} void fun() const&{gun();} void fun() &&{std::move(*this).gun();} // <-- is this correct? or is there a better option }; int main(){ A a; a.fun(); // prints gun const& A().fun(); // prints gun&& } Something doesn't

Is std::move(*this) a good pattern?

白昼怎懂夜的黑 提交于 2019-12-20 11:05:28
问题 In order to make this code with C++11 reference qualifiers work as expected I have to introduce a std::move(*this) that doesn't sound right. #include<iostream> struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun() &&{std::cout << "gun&&" << std::endl;} void fun() const&{gun();} void fun() &&{std::move(*this).gun();} // <-- is this correct? or is there a better option }; int main(){ A a; a.fun(); // prints gun const& A().fun(); // prints gun&& } Something doesn't

How to use move semantics with std::string during function return? [duplicate]

故事扮演 提交于 2019-12-20 09:55:31
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C++11 rvalues and move semantics confusion What I think is correct is std::string GetLine() { std::string str; std::getline(std::cin, str); return std::move(str); } But at this link http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html ( check the header part Returning an explicit rvalue-reference from a function) which is #1 google search hit for move semantics shows a similar

Efficient use of move semantics together with (N)RVO

泄露秘密 提交于 2019-12-20 09:46:01
问题 Let's say I want to implement a function that is supposed to process an object and return a new (possibly changed) object. I would like to do this as efficient as possible in C+11. The environment is as follows: class Object { /* Implementation of Object */ Object & makeChanges(); }; The alternatives that come to my mind are: // First alternative: Object process1(Object arg) { return arg.makeChanges(); } // Second alternative: Object process2(Object const & arg) { return Object(arg)

Correct use of `= delete` for methods in classes

﹥>﹥吖頭↗ 提交于 2019-12-20 08:48:55
问题 Is the following snipplet correct for un-defining all otherwise generated methods and constructors for a class? struct Picture { // 'explicit': no accidental cast from string to Picture explicit Picture(const string &filename) { /* load image from file */ } // no accidental construction, i.e. temporaries and the like Picture() = delete; // no copy Picture(const Picture&) = delete; // no assign Picture& operator=(const Picture&) = delete; // no move Picture(Picture&&) = delete; // no move

Move constructor suppressed by comma operator

老子叫甜甜 提交于 2019-12-19 12:51:15
问题 This program: #include <iostream> struct T { T() {} T(const T &) { std::cout << "copy constructor "; } T(T &&) { std::cout << "move constructor "; } }; int main() { ([](T t) -> T { return t; })({}); std::cout << '\n'; ([](T t) -> T { return void(), t; })({}); std::cout << '\n'; ([](T t) -> T { return void(), std::move(t); })({}); std::cout << '\n'; } when compiled by gcc-4.7.1 outputs (link): move constructor copy constructor move constructor Why does the comma operator have this effect? The

Does the inverse of std::move exist? [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-19 09:46:44
问题 This question already has answers here : Function dual to std::move? (3 answers) Closed 6 years ago . std::move can be used to explicitly allow move semantics when the move wouldn't be already allowed implicitly (such as often when returning a local object from a function). Now, I was wondering ( esp. in the context of local return and implicit move there ), if there is such a thing as the inverse of std::move that will prevent moving the object (but still allow copying). Does this even make

When will adding a move constructor and a move assignment operator really start make a difference?

让人想犯罪 __ 提交于 2019-12-19 09:09:34
问题 Considering the high quality of today's compilers regarding return value optimization (both RVO and NRVO), I was wondering at what class complexity it's actually meaningful to start adding move constructors and move assignment operators. For instance, for this really_trivial class, I just assume that move semantics cannot offer anything more than RVO and NRVO already does when copying around instances of it: class really_trivial { int first_; int second_; public: really_trivial(); ... };

When will adding a move constructor and a move assignment operator really start make a difference?

痴心易碎 提交于 2019-12-19 09:09:09
问题 Considering the high quality of today's compilers regarding return value optimization (both RVO and NRVO), I was wondering at what class complexity it's actually meaningful to start adding move constructors and move assignment operators. For instance, for this really_trivial class, I just assume that move semantics cannot offer anything more than RVO and NRVO already does when copying around instances of it: class really_trivial { int first_; int second_; public: really_trivial(); ... };

How to improve std::vector parameter passing (move semantics?)

谁说我不能喝 提交于 2019-12-19 08:29:10
问题 It seems that I cannot completely understand move semantics: I want to fill an std::vector (member of a class) from an external function. Currently, I have something like: void fillVector(MyClass & myclass) { std::vector<int> vec; /* Filling vec */ // ... myclass.setVector(vec); } class MyClass { public: setVector(const std::vector<int> & v) { v_ = v;} private: std::vector<int> v_; }; int main() { MyClass myclass; fillVector(myclass); /* Use myclass.v_ somehow */. } I had this code for a long