move-semantics Questions

2

Are the move semantics used in Example A necessary, and which struct is superior? Example A: struct A { std::string a; A( std::string a ) : a( std::move(a) ){ } }; Example B: struct B { std...
Madalene asked 4/4, 2017 at 1:24

4

Solved

In C++, is it OK to steal resources from a map that I do not need afterwards anymore? More precisely, assume I have a std::map with std::string keys and I want to construct a vector out of it by st...
Neophyte asked 22/2, 2020 at 7:15

3

Solved

Consider the code below void foo(std::vector<int> v) { //do something here } //calling the function vector<int> v1 = {1,2,3}; foo(std::move(v1)); My question is, isn't the function ...
Respiration asked 20/1, 2020 at 20:52

7

Solved

I'm a bit confused regarding the difference between push_back and emplace_back. void emplace_back(Type&& _Val); void push_back(const Type& _Val); void push_back(Type&& _Val); ...
Monteria asked 29/11, 2010 at 12:4

3

Solved

http://www.drdobbs.com/cpp/practical-c-error-handling-in-hybrid-env/197003350?pgno=4 In this article Herb Sutter explains that throwing an exception requires a copy of the exception as it's create...
Plenty asked 25/8, 2012 at 13:18

2

Solved

I was reading a book about data structure implemented in C++, I dont understand a code snippet, it's part of vector class void push_back(object &&x) { //do something objects[size++] = s...

3

Solved

In C++11, value parameters (and other values) enjoy implicit move when returned: A func(A a) { return a; // uses A::A(A&&) if it exists } At least in MSVC 2010, rvalue reference paramet...
Pyromania asked 19/3, 2012 at 22:49

2

Solved

Removing the comment from following code gives a compile time error. It seems that defining the destructor in derived class is causing the copy constructor to be called in emplace_back #include &l...
Tendril asked 9/10, 2019 at 22:30

1

Solved

As said by cppreference.com, Maps are usually implemented as red-black trees. So moving a std::map is just moving the pointer to the root node + other information such as size. Why is std::ma...
Larrisa asked 31/7, 2019 at 21:54

1

Solved

Taking as example void f(B b, A&& a) {...} B g(B b, A a) {...} int main() { B b; A a; f(g(b, a), std::move(a)); } I presume this would be valid code seeing as an std::move() is merel...
Muslin asked 21/7, 2019 at 10:27

1

The standard mandates that the move assignment operator of optional ... constexpr optional& operator=( optional&& other ) [...] shall not participate in overload resolution unless...
Sello asked 18/3, 2019 at 20:49

2

Solved

Consider the following class Buffer, which contains an std::vector object: #include <vector> #include <cstddef> class Buffer { std::vector<std::byte> buf_; protected: Buffer(s...
Wieland asked 14/7, 2019 at 14:44

2

Solved

Inspired by the post Why does destructor disable generation of implicit move methods?, I was wondering if the same is true for the default virtual destructor, e.g. class WidgetBase // Base class o...
Extort asked 27/11, 2015 at 12:4

1

Solved

Standard specifies that STL containers, after begin moved (in this case we talk about std::move that enables move construction / assignment), are in valid, but unspecified state. I belive th...
Winna asked 12/7, 2019 at 13:53

1

Solved

I have run into a problem that a move constructor of a superclass did not get invoked properly when its subclass has an explicitly defaulted destructor. The move constructor does get invoked when t...
Shopper asked 10/7, 2019 at 10:10

4

Solved

An unique_ptr cannot be pushed back into a std::vector since it is non-copyable, unless std::move is used. However, let F be a function that returns a unique_ptr, then the operation std::vector::pu...
Rheology asked 7/7, 2019 at 20:37

5

Solved

When implementing move constructors and move assignment operators, one often writes code like this: p = other.p; other.p = 0; The implicitly defined move operations would be implemented with cod...
Shogunate asked 26/2, 2012 at 10:55

1

Solved

Let's consider the following code: class X { std::vector<int> _v; public: X(std::vector<int>&& v): _v(std::move(v)) {} }; The compiler calls this constructor only for objec...
Popelka asked 30/6, 2019 at 18:24

1

I originally assumed that it is bad practice to move an l-value reference parameter. Is this indeed commonly agreed by the C++ developer community? When I call a function that has an R-value refer...
Welterweight asked 24/6, 2019 at 13:20

1

Solved

The following code using vptr = std::vector<std::unique_ptr<int>>; auto m = std::unordered_map<int, std::any>{}; m.try_emplace(0, move(vptr{})); Fails to compile, complaining a...
Pentacle asked 29/5, 2019 at 22:12

2

Solved

I'm trying to understand Item 17 from "Effective Modern C++" about special member function generation so I was trying some examples and am trying to reason about some behavior. In the book it says:...
Litchfield asked 21/5, 2019 at 15:48

2

I have a class in my C++ code which has its own move constructor. A simplified version is shown here: class myClass { //In this example, myClass must manually manage allocating //and freeing a m...
Enchondroma asked 19/5, 2019 at 0:37

2

Solved

I am currently learning more about all the c++11/14 features and wondering when to use std::move in function calls. I know I should not use it when returning local variables, because this bre...
Cornall asked 26/4, 2019 at 10:1

3

Solved

I have some code in which I want to make absolutely sure that a moved-from std::vector will not leave secret data around (think about crypto key management). In my class' move constructor, I do som...
Female asked 7/3, 2019 at 23:1

6

Solved

I'm trying to understand rvalue references and move semantics of C++11. What is the difference between these examples, and which of them is going to do no vector copy? First example: std::vector&lt...
Heliacal asked 13/2, 2011 at 20:28

© 2022 - 2024 — McMap. All rights reserved.