move-semantics

Can I move-assign a std::map's contents into another std::map?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-29 07:22:09
问题 Is it possible to insert the contents of a temporary std::map temp into another std::map m by using move semantics, such that the values from the temporary are not copied and are reused? Let's say one has: std::map<int, Data> temp; std::map<int, Data> m; One way of copying values from temp into m is: m.insert(temp.begin(),temp.end()); How can I move the temp elements into m , instead of copying? 回答1: HINT: Read the update first! The current C++11 standard and the C++14 draft do not provide a

Should I delete the move constructor and the move assignment of a smart pointer?

不羁岁月 提交于 2019-12-29 03:04:46
问题 I'm implementing a simple smart pointer, which basically keeps track of the number of references to a pointer that it handles. I know I could implement move semantics, but I don't think it makes sense as copying a smart pointer is very cheap. Especially considering that it introduces opportunities to produce nasty bugs. Here's my C++11 code (I omitted some inessential code). General comments are welcome as well. #ifndef SMART_PTR_H_ #define SMART_PTR_H_ #include <cstdint> template<typename T>

Is specializing std::swap deprecated now that we have move semantics? [duplicate]

雨燕双飞 提交于 2019-12-28 12:12:41
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Move semantics == custom swap function obsolete? This is how std::swap looks like in C++11: template<typename T> void swap(T& x, T& y) { T z = std::move(x); x = std::move(y); y = std::move(z); } Do I still have to specialize std::swap for my own types, or will std::swap be as efficient as it gets, provided that my class defines a move constructor and a move assignment operator, of course? 回答1: The specialization

Move-only version of std::function

…衆ロ難τιáo~ 提交于 2019-12-27 13:37:28
问题 Because std::function is copyable, the standard requires that callables used to construct it also be copyable: n337 (20.8.11.2.1) template<class F> function(F f); Requires: F shall be CopyConstructible. f shall be Callable (20.8.11.2) for argument types ArgTypes and return type R . The copy constructor and destructor of A shall not throw exceptions.` This implies that it is not possible to form an std::function from a non-copyable bind object or a lambda that captured a move-only type such as

Move-only version of std::function

孤者浪人 提交于 2019-12-27 13:35:19
问题 Because std::function is copyable, the standard requires that callables used to construct it also be copyable: n337 (20.8.11.2.1) template<class F> function(F f); Requires: F shall be CopyConstructible. f shall be Callable (20.8.11.2) for argument types ArgTypes and return type R . The copy constructor and destructor of A shall not throw exceptions.` This implies that it is not possible to form an std::function from a non-copyable bind object or a lambda that captured a move-only type such as

Forward or Move

好久不见. 提交于 2019-12-25 04:14:54
问题 Are these valid usage of move and forward? Are f3 and f4 the same? Is it dangerous to do so? Thank you! #include <utility> class A {}; A f1() { A a; return a; // Move constructor is called } A f2(A&& a) { return a; // Copy constructor is called, which is what I try to avoid. } A f3(A&& a) { return std::forward<A&&>(a); // Move constructor is called } A f4(A&& a) { return std::move(a); // Move constructor is called } 回答1: Use std::forward with a universal reference , i.e. a template <typename

Returning std::move of a local variable [duplicate]

♀尐吖头ヾ 提交于 2019-12-24 17:04:56
问题 This question already has answers here : Using std::move() when returning a value from a function to avoid to copy (3 answers) Closed 6 years ago . Let there be a class A with a move constructor. Consider this: A get() { A a; return std::move( a ); } // later in the code A aa = get(); Here the explicit call to std:move forces the move constructor of A to be called thus it might inhibit the return value optimization in while calling get() . Thus it is said the a better implementation of get()

How to make sure an object will really be moved from?

ぃ、小莉子 提交于 2019-12-24 01:15:00
问题 Consider the following code, which tries to move-construct a shared_ptr , but due to a mistake appears to copy-construct it: #include <utility> #include <cassert> #include <memory> int main() { const auto x=std::make_shared<int>(4325); // can't be moved from const std::shared_ptr<int> y(std::move(x)); // silently copy-constructs assert(x==nullptr); // fails } Here the fact that due to x being const , y was copy-constructed instead of move-construction will only be detected at runtime. Is

Special member functions in C++0x

落爺英雄遲暮 提交于 2019-12-23 19:47:43
问题 The Wikipedia article about special member functions doesn't contain any reference to move constructors and move assignment operators. I would like to update the entry but I'm not sure what the 0x standard says. What are the rules regarding these two functions? Are they automatically generated by the compiler and if so when? Edit: I've updated the Wikipedia page, if anyone feels like it please help the community by editing it into shape (if needed). 回答1: Keeping in mind C++0x isn't quite

Moving a vector of unique_ptr<T> [duplicate]

梦想与她 提交于 2019-12-23 16:36:59
问题 This question already has answers here : Should a move constructor take a const or non-const rvalue reference? (4 answers) Closed 4 years ago . So I have a situation where I need to store a vector of an abstract type, as I understand this requires the usage of a vector of unique_ptrs or similar. So in order to move about instances of the class which contains the vector of unique_ptrs, I need to define a move constructor which I have done. However as demonstrated by the example below, this