move-semantics

Is there any case where a return of a RValue Reference (&&) is useful?

谁说胖子不能爱 提交于 2019-12-05 07:54:59
Is there a reason when a function should return a RValue Reference ? A technique, or trick, or an idiom or pattern? MyClass&& func( ... ); I am aware of the danger of returning references in general, but sometimes we do it anyway, don't we ( T& T::operator=(T) is just one idiomatic example). But how about T&& func(...) ? Is there any general place where we would gain from doing that? Probably different when one writes library or API code, compared to just client code? There are a few occasions when it is appropriate, but they are relatively rare. The case comes up in one example when you want

Static arrays VS. dynamic arrays in C++11

早过忘川 提交于 2019-12-05 06:22:45
I know that it's a very old debate that has already been discussed many times all over the world. But I'm currently having troubles deciding which method I should use rather than another between static and dynamic arrays in a particular case. Actually, I woudn't have used C++11, I would have used static arrays. But I'm now confused since there could be equivalent benefits with both. First solution: template<size_t N> class Foo { private: int array[N]; public: // Some functions } Second solution: template<size_t N> class Foo { private: int* array; public: // Some functions } I can't happen to

C++11 constructor argument: std::move and value or std::forward and rvalue reference

社会主义新天地 提交于 2019-12-05 04:00:39
问题 Which of the below two should be preferred and why? struct X { Y data_; explicit X(Y&& data): data_(std::forward<Y>(data)) {} }; vs struct X { Y data_; explicit X(Y data): data_(std::move(data)) {} }; 回答1: The two variants differ in functionality. The following statements work for the second one–but not for the first one: Y y; X x(y); If you are looking for the same functionality, the two variants should look as follows: struct X { Y data_; explicit X(const Y& data) : data_(data) { } explicit

Why std::make_move_iterator works on vector<string> but not on vector<int>

浪尽此生 提交于 2019-12-05 03:39:37
I was expecting that std::make_move_iterator will always move contents, but it seems not. It looks like it is moving elements in vector<string> but not in vector<int> . See the below code snippet: #include <iostream> #include <iterator> #include <string> #include <vector> void moveIntVector() { std::cout << __func__ << std::endl; std::vector<int> v1; for (unsigned i = 0; i < 10; ++i) { v1.push_back(i); } std::vector<int> v2( std::make_move_iterator(v1.begin() + 5), std::make_move_iterator(v1.end())); std::cout << "v1 is: "; for (auto i : v1) { std::cout << i << " "; } std::cout << std::endl;

Is move assignment via destruct+move construct safe?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 03:36:43
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 standard C++11? Only if you never, ever derive a type from this class. If you do, this will turn the

How can I reuse a box that I have moved the value out of?

左心房为你撑大大i 提交于 2019-12-05 02:03:16
I have some non-copyable type and a function that consumes and (maybe) produces it: type Foo = Vec<u8>; fn quux(_: Foo) -> Option<Foo> { Some(Vec::new()) } Now consider a type that is somehow conceptually very similar to Box : struct NotBox<T> { contents: T } We can write a function that temporarily moves out contents of the NotBox and puts something back in before returning it: fn bar(mut notbox: NotBox<Foo>) -> Option<NotBox<Foo>> { let foo = notbox.contents; // now `notbox` is "empty" match quux(foo) { Some(new_foo) => { notbox.contents = new_foo; // we put something back in Some(notbox) }

How can I take an item from a Vec in Rust?

拈花ヽ惹草 提交于 2019-12-05 01:55:56
I'm looking for a method that consumes a Vec and returns one element, without the overhead of restoring Vec 's invariants the way remove and swap_remove do: fn take<T>(vec: Vec<T>, index: usize) -> Option<T> However, I can't find such a method. Am I missing something? Is this actually unsafe or impossible? This is a different question from Built in *safe* way to move out of Vec<T>? There the goal was a remove method that didn't panic on out of bounds access and returned a Result . I'm looking for a method that consumes a Vec and returns one of the elements. None of the answers to the above

Sink arguments and move semantics for functions that can fail (strong exception safety)

為{幸葍}努か 提交于 2019-12-05 01:08:52
I have a function that operates on a big chunk of data passed in as a sink argument. My BigData type is already C++11-aware and comes with fully functional move constructor and move assignment implementations, so I can get away without having to copy the damn thing: Result processBigData(BigData); [...] BigData b = retrieveData(); Result r = processBigData(std::move(b)); This all works perfectly fine. However, my processing function may fail occasionally at runtime resulting in an exception. This is not really a problem, since I can just fix stuff and retry: BigData b = retrieveData(); Result

Are there any use cases for a class which is copyable but not movable?

大兔子大兔子 提交于 2019-12-04 23:41:15
After reading this recent question by @Mehrdad on which classes should be made non-movable and therefore non-copyable , I starting wondering if there are use cases for a class which can be copied but not moved . Technically, this is possible: struct S { S() { } S(S const& s) { } S(S&&) = delete; }; S foo() { S s1; S s2(s1); // OK (copyable) return s1; // ERROR! (non-movable) } Although S has a copy constructor, it obviously does not model the CopyConstructible concept, because that is in turn a refinement of the MoveConstructible concept, which requires the presence of a (non-deleted) move

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

瘦欲@ 提交于 2019-12-04 23:07:50
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++11, as implemented in Microsoft Visual C++ 2012, the internal details of a function can modify its