move-semantics

Default copy constructor and assignment for class with move constructor and assignment

▼魔方 西西 提交于 2019-12-18 12:52:24
问题 Let's say I have this class: class Test { public: Test(); }; AFAIK, compiler provides default copy constructor and assignment operators, which assign every member of other instance to the current instance. Now I add move constructor and assignment: class Test { public: Test(); Test(Test&& other); Test& operator=(Test&& other); }; Does this class still contain compiler-generated copy constructor and assignment operators, or I need to implement them? Edit . This is my test: class Test { public:

When will a C++11 compiler make RVO and NRVO outperform move semantics and const reference binding?

ε祈祈猫儿з 提交于 2019-12-18 12:17:42
问题 Consider the case when "whole" objects with move semantics enabled are returned from functions, as with std::basic_string<> : std::wstring build_report() const { std::wstring report; ... return report; } Can I then realistically be expected to make the "best" choice whether to use the returned string with move semantics, as in const std::wstring report(std::move(build_report())); or if I should rely on (N)RVO to take place with const std::wstring report(build_report()); or even bind a const

Implementing Move Constructor by Calling Move Assignment Operator

送分小仙女□ 提交于 2019-12-18 10:44:23
问题 The MSDN article, How to: Write a Move Constuctor, has the following recommendation. If you provide both a move constructor and a move assignment operator for your class, you can eliminate redundant code by writing the move constructor to call the move assignment operator. The following example shows a revised version of the move constructor that calls the move assignment operator: // Move constructor. MemoryBlock(MemoryBlock&& other) : _data(NULL) , _length(0) { *this = std::move(other); }

How can moved objects be used? [duplicate]

醉酒当歌 提交于 2019-12-18 08:09:11
问题 This question already has answers here : What can I do with a moved-from object? (2 answers) Closed 3 years ago . After moving an object, it must be destructable: T obj; func(std::move(obj)); // don't use obj and let it be destroyed as normal But what else can be done with obj? Could you move another object into it? T obj; func(std::move(obj)); obj = std::move(other); Does this depend on the exact type? (E.g. std::vector could make specific guarantees you can't rely on for all T.) Is it

Why is move constructor not picked when returning a local object of type derived from the function's return type?

霸气de小男生 提交于 2019-12-18 05:43:12
问题 The following code is rejected by both Clang and GCC (trunk versions): #include <memory> struct Base { Base() = default; Base(Base const&) = delete; Base(Base&&) = default; }; struct Derived : Base { Derived() = default; Derived(Derived const&) = delete; Derived(Derived&&) = default; }; auto foo() -> Base { Derived d; return d; // ERROR HERE } Causing the following error: prog.cc: In function 'Base foo()': prog.cc:21:12: error: use of deleted function 'Base::Base(const Base&)' return d; ^

Move or swap a stringstream

一个人想着一个人 提交于 2019-12-18 04:52:56
问题 I want to move a stringstream, in the real world application I have some stringstream class data member, which I want to reuse for different string's during operation. stringstream does not have a copy-assignment or copy constructor, which makes sense. However, according to cppreference.com and cplusplus.com std::stringstream should have a move assignment and swap operation defined. I tried both, and both fail. Move assignment #include <string> // std::string #include <iostream> // std::cout

Making swap faster, easier to use and exception-safe

独自空忆成欢 提交于 2019-12-18 03:30:18
问题 I could not sleep last night and started thinking about std::swap . Here is the familiar C++98 version: template <typename T> void swap(T& a, T& b) { T c(a); a = b; b = c; } If a user-defined class Foo uses external ressources, this is inefficient. The common idiom is to provide a method void Foo::swap(Foo& other) and a specialization of std::swap<Foo> . Note that this does not work with class templates since you cannot partially specialize a function template, and overloading names in the

Making swap faster, easier to use and exception-safe

半世苍凉 提交于 2019-12-18 03:29:58
问题 I could not sleep last night and started thinking about std::swap . Here is the familiar C++98 version: template <typename T> void swap(T& a, T& b) { T c(a); a = b; b = c; } If a user-defined class Foo uses external ressources, this is inefficient. The common idiom is to provide a method void Foo::swap(Foo& other) and a specialization of std::swap<Foo> . Note that this does not work with class templates since you cannot partially specialize a function template, and overloading names in the

Implicitly treating returned lvalue as rvalue

可紊 提交于 2019-12-18 03:05:27
问题 12.8 Copying and moving class objects [class.copy] §31 and §32 say: in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value When the criteria for elision of a copy operation are met or would be

When should compiler generate move constructor?

巧了我就是萌 提交于 2019-12-17 23:28:13
问题 I use VS11 and use following: class ContextWrapper { public: ContextWrapper() { } //it should be defaulted I *guess* in order to have automatic move constructor ? // no support in VS11 for that now Context* GetContext() { return this->context.get(); } void SetContext(std::unique_ptr<Context> context) { this->context = std::move(context); } //ContextWrapper(ContextWrapper&& other): context(std::move(other.context)) //{ //} // I would like this to be generated by the compiler private: