move-semantics

why should all iterators / iterator adaptors not-movable in C++11?

拥有回忆 提交于 2019-12-04 22:46:27
In this question discussed When to make a type non-movable in C++11 and I discovered Scott Meyers had similar question on comp.std.c++ , where SG listed below class types are not movable in C++11 libeary. all mutex types(recursive_mutex , timed_mutex, recursive_timed_mutex, condition_variable type_info error_category locale::facet random_device seed_seq reference_wrapper duration time_point - all iterators / iterator adaptors ios_base basic_istream::sentry basic_ostream::sentry all atomic types once_flag The question is why is all iterators / iterator adaptors not-movable ? That post, from a

const_cast and std::move to remove constness from non-reference

隐身守侯 提交于 2019-12-04 22:30:51
I have an external library which I can not modify. The library declares a template function that for some reason returns const non-reference object: template<class C> const C foo(); I have another external library which I can not modify too. The library declares a class that is non-copyable and has a move constructor from a non-const object only: struct bar { bar(); bar(const bar&)=delete; bar(bar&&); }; Now I need to use foo<bar> . A simple usage: bar buz() { return foo<bar>(); } fails with main.cpp: In function 'bar buz()': main.cpp:13:21: error: use of deleted function 'bar::bar(const bar&)

Move semantics for a conversion operator

人盡茶涼 提交于 2019-12-04 22:29:25
问题 What's the syntax for a movable conversion operator? I have a wrapper that wraps around obj , which has an obj conversion operator: class wrap { public: operator obj() { ... } private: obj data_; }; How can I find out whether data_ should be copied or moved? 回答1: The syntax for that would be something like this: class wrap { public: operator obj() const & { ... } //Copy from me. operator obj() && { ... } //Move from me. private: obj data_; }; The first version will be called when the second

Insert map entry by r-value moving of mapped_type

99封情书 提交于 2019-12-04 17:37:26
问题 I have a map with a (fairly) simple key-type and a complex mapped-type, like so: map<string, vector<string>> myMap; If I have a vector<string> in hand, is it possible to insert an entry into the map which copies the key but moves the mapped-value? That is, is there some way to do: string key = "Key"; vector<string> mapped; for (int i = 0; i < 1000; ++i) mapped.push_back("Some dynamic string"); // Insert by moving mapped; I know I'm done with it myMap.insert(make_pair(key, move(mapped))); //

Can I write both copy and move assignment operators for a class?

左心房为你撑大大i 提交于 2019-12-04 17:06:45
These are my prototypes, MyClass& operator=(MyClass rhs); // copy assignment MyClass& operator=(MyClass &&rhs); // move assignment But when I call MyClass a, b; a = std::move(b); , there is an error. 556 IntelliSense: more than one operator "=" matches these operands: function "MyClass::operator=(MyClass rhs)" function "MyClass::operator=(MyClass &&rhs)" operand types are: MyClass = MyClass And the compiler returns: Error 56 error C2593: 'operator =' is ambiguous Overload resolution is ambiguous because when you pass an rvalue, both MyClass and MyClass && can be directly initialised by it. If

Make a class non-copyable *and* non-movable

妖精的绣舞 提交于 2019-12-04 17:03:09
问题 Before C++11, I could use this to make a class non-copyable: private: MyClass(const MyClass&); MyClass& operator=(const MyClass&); With C++11, I can do it like this instead: MyClass(const MyClass&) = delete; MyClass& operator=(const MyClass&) = delete; When using the class with the deleted copy and assignment, is there a chance that a default move operator is generated? And the class is not exactly copied, but moved (which is sort of similar) after all? So, do I have to do this to prevent

Should use unique_ptr to more easily implement “move” semantics?

南笙酒味 提交于 2019-12-04 16:51:39
Edit: made Foo and Bar a little less trivial, and direct replacement with shared_ptr<> more difficult. Should unique_ptr<> be used as an easier way to implement move semantics? For a class like class Foo { int* m_pInts; bool usedNew; // other members ... public: Foo(size_t num, bool useNew=true) : usedNew(useNew) { if (usedNew) m_pInts = new int[num]; else m_pInts = static_cast<int*>(calloc(num, sizeof(int))); } ~Foo() { if (usedNew) delete[] m_pInts; else free(m_pInts); } // no copy, but move Foo(const Foo&) = delete; Foo& operator=(const Foo&) = delete; Foo(Foo&& other) { *this = std::move

Why have move semantics?

眉间皱痕 提交于 2019-12-04 15:57:51
问题 Let me preface by saying that I have read some of the many questions already asked regarding move semantics. This question is not about how to use move semantics, it is asking what the purpose of it is - if I am not mistaken, I do not see why move semantics is needed. Background I was implementing a heavy class, which, for the purposes of this question, looked something like this: class B; class A { private: std::array<B, 1000> b; public: // ... } When it came time to make a move assignment

Is it useless to declare a local variable as rvalue-reference, e.g. T&& r = move(v)?

て烟熏妆下的殇ゞ 提交于 2019-12-04 15:20:07
问题 Could you guys give me an illustrative example under certain circumstance to prove the following statements are useful and necessary? AnyTypeMovable v; AnyTypeMovable&& r = move(v); 回答1: No, AnyTypeMovable&& r = move(v); here is not useful at all. Consider the following code: #include <iostream> #include <vector> class MyMovableType { int i; public: MyMovableType(int val): i(val){} MyMovableType(MyMovableType&& r) { this->i = r.i; r.i = -1; } MyMovableType(const MyMovableType& r){ this->i = r

Providing correct move semantics

↘锁芯ラ 提交于 2019-12-04 14:02:29
I am currently trying to figure out how to do move semantics correctly with an object which contains a pointer to allocated memory. I have a big datastructure, which contains an internal raw pointer to the actual storage (for efficiency reasons). Now I added a move constructor and move operator=() . In these methods I am std::move() ing the pointer to the new structure. However I am not sure what to do with the pointer from the other structure. Here is a simple example of what I am doing: class big_and_complicated { // lots of complicated code }; class structure { public: structure() : m_data(