move-semantics Questions

3

Solved

Say I have two vectors and I move one unto the other, v1 = std::move(v2); will v2 still be in a usable state after this?
Sprinkling asked 28/10, 2011 at 13:46

3

Solved

In the following C++11+ code which return statement construction should be preferred? #include <utility> struct Bar { }; struct Foo { Bar bar; Bar get() && { return std::move...
Arbela asked 28/1, 2018 at 11:35

2

Lets take two structs/classes struct C1{ C1(){}; C1(C1&){std::cout<<"copy"<<std::endl;} C1(C1&&){std::cout<<"move"<<std::endl;}}; struct C2{ C1 c; C2(){};...
Unblessed asked 28/1, 2018 at 19:38

2

Solved

I have read many articles about move constructor (even on stack) but i didn't find anywhere an exact explanation about how this works (how is transfer pointer to a temporary object and saved if thi...
Inchoation asked 17/1, 2018 at 13:40

3

Solved

I cannot figure out why in the last case is the move constructor called when copy elision is enabled (or even mandatory such as in C++17): class X { public: X(int i) { std::clog << "conver...

3

Solved

For context, the actual class I'm working with is considerably more complicated and larger than what I'm showing here, but I'm using this just as an example. struct Vector { int x, y; Vector() :...
Newmarket asked 5/1, 2018 at 17:26

2

Solved

Let's take std::pair<T1, T2> as an example. It has the following two constructors: constexpr pair( const T1& x, const T2& y ); // #1 template< class U1, class U2 > constexpr pa...

1

Solved

AFAIK non-mutable lambdas capture variables as const. This makes me wonder why can they still be moved? auto p = std::make_unique<int>(0); auto f = [p = std::move(p)](){ p->reset(); }; //...
Paling asked 12/12, 2017 at 14:40

1

Solved

I am reading the second edition of the Rust Book and I found the following sample in the iterators section: let v1 = vec![1, 2, 3]; let v1_iter = v1.iter(); for val in v1_iter { println!("Got: {...
Jorgenson asked 11/12, 2017 at 19:28

4

Solved

For some time this has been confusing me. And I've not been able to find a satisfactory answer thus far. The question is simple. When does a move assignment operator get called, and when does a mov...
Cleo asked 30/4, 2015 at 15:2

1

Solved

The following minimal working example compiles when the code under option 1 or option 2 is used, but does not compile when the code under option 3 is used. I was under the assumption that emplace_b...
Soudan asked 15/11, 2017 at 17:32

8

Solved

As far as I understand one of the purposes of adding move semantics is to optimize code by calling special constructor for copying "temporary" objects. For example, in this answer we see that it ca...
Moccasin asked 17/2, 2011 at 16:38

2

Solved

Consider following diamond-like multiple inheritance: class base; class d1 : virtual public base; class d2 : virtual public base class d3 : public d1, public d2; base is a move-only class (havin...
Tiresias asked 5/11, 2017 at 8:51

3

I have a std::vector of objects of a certain class A. The class is non-trivial and has copy constructors and move constructors defined. std::vector<A> myvec; If I fill-up the vector with ...
Eidetic asked 3/11, 2011 at 21:3

4

Solved

To improve efficiency of std::vector<T>, it's underlying array needs to be pre-allocated and sometimes re-allocated. That, however, requires the creation and later moving of objects of type T...
Dissepiment asked 10/12, 2013 at 6:37

4

Solved

I'm playing with move constructors and move assignments and i've stumbled on this problem. First code: #include <iostream> #include <utility> class Foo { public: Foo() {} Foo(Foo&...
Deadlock asked 26/11, 2014 at 0:33

4

Solved

I was surprised this didn't show up in my search results, I thought someone would've asked this before, given the usefulness of move semantics in C++11: When do I have to (or is it a good idea for ...
Tillio asked 13/1, 2013 at 11:5

5

Solved

I am trying to understand move semantics, rvalue references, std::move, etc. I have been trying to figure out, by searching through various questions on this site, why passing a const std::string &...
Bowshot asked 3/9, 2017 at 23:37

2

Solved

I have an class A: class A { int value1; int value2; std::string text; public: A(int value1, int value2, std::string text) : value1(value1), value2(value2), text(text) { } }; And some "co...
Paginate asked 27/9, 2017 at 15:56

1

I have the following C++(11) code: #include <mutex> void unlock(std::unique_lock<std::mutex> && ulock) { } int main(void) { std::mutex m; std::unique_lock<std::mutex>...
Canara asked 29/8, 2017 at 19:36

2

Solved

Consider the following code: #include <iostream> #include <vector> using namespace std; class A { public: A(int) { cout << "int" << endl; } A(A&&) { cout &...

6

Solved

In the assignment operator of a class, you usually need to check if the object being assigned is the invoking object so you don't screw things up: Class& Class::operator=(const Class& rhs)...
Bow asked 17/2, 2012 at 2:41

5

Solved

Recently, many questions pop up on how to provide your own swap function. With C++11, std::swap will use std::move and move semantics to swap the given values as fast as possible. This, of course, ...
Mader asked 20/6, 2011 at 19:30

3

Solved

I'm trying to use move semantics (just as an experiment). Here is my code: class MyClass { public: MyClass(size_t c): count(c) { data = new int[count]; } MyClass( MyClass&& src) : c...
Iveson asked 26/2, 2014 at 18:33

1

Solved

The function std::mem::drop in Rust moves its argument and then destroys it by going out of scope. My attempt at writing a similar function in C++ looks like this: template <typename T, typena...
Flofloat asked 27/7, 2017 at 22:38

© 2022 - 2024 — McMap. All rights reserved.