Questions tagged [stdmove]

117 questions
2
votes
2 answers

C++ moving a unique_ptr to a struct member

I have the following program - #include #include class Person { public: Person(const std::string& name): name(name) { } ~Person() { std::cout << "Destroyed" << std::endl; } std::string…
Jai Prabhu
  • 245
  • 3
  • 10
1
vote
1 answer

How to prevent unique pointers from overlapping

I'm trying to build a timer in C++. In my Timer class there are two Date objects that hold a std::unique_ptr pointer. After I std::move the second unique_ptr in the second Date object in the following piece of code, it points to the same…
loremol
  • 25
  • 4
1
vote
1 answer

C++20 Compiler Error in Visual Studio 2022 Community: Resolving Internal Error C1001

I am facing an issue with C++20 in Visual Studio 2022 Community. The code works correctly in G++ but I get an internal compiler error with MSVC. Here is the code: #include struct Foo { float* data; Foo(float * const * const &…
Matt
  • 179
  • 6
1
vote
2 answers

Why does std::move cause SEGFAULT in this case?

Why does std::move() cause a SEGFAULT in this case? #include struct Message { std::string message; }; Message * Message_Init(std::string message) { Message * m = (Message*)calloc(1, sizeof(Message)); m->message =…
1
vote
1 answer

Does std::vector "move" implicitly?

Consider the following code: #include #include #include class Domain { public: enum class fieldname { pos_x, pos_y }; std::unordered_map>…
P. Nair
  • 79
  • 7
1
vote
1 answer

return std::move a class with a unique_ptr member

Why can't I return a class containing a std::unique_ptr, using std::move semantics (I thought), as in the example below? I thought that the return would invoke the move ctor of class A, which would std::move the std::unique_ptr. (I'm using GCC 11.2,…
voxoid
  • 1,164
  • 1
  • 14
  • 31
1
vote
1 answer

Mixing Rvalue and LValue reference

I'm trying to better understand LValue, RValue, and how std::move works. I have the following code #include class A { public: A() = default; A(std::string&& aString): myString(std::move(aString)) {} std::string myString; }; class…
1
vote
1 answer

using an initializer list where the values contain unique_ptr member variables

I have a class that contains a unique_ptr. I want to place instances of this class inside a container (specifically std::map). This works using std::move and .emplace however, I would like to perform all this initialization within the container's…
J'e
  • 3,014
  • 4
  • 31
  • 55
1
vote
1 answer

getting invalid ouput after std::move of an element from std::list

I'm trying to understand about std::move. In my code I'm moving an element from std::list where struct Data internally contains two std::string fields, but I'm not getting expected output. This is my code: #include #include…
Harry
  • 2,177
  • 1
  • 19
  • 33
1
vote
2 answers

Why move ctor is not called in this case?

#include using namespace std; class Test { public: Test(string value){ cout<<"Ctor "<
HDenied
  • 37
  • 6
1
vote
2 answers

Can I avoid copies when returning multiple values, while keeping my return type?

If we write the following function: auto foo() { Foo foo { /* ... */ }; do_stuff(foo); return foo; } then NRVO should kick in, so that foo does not get copied on return. Now suppose I want to return two different values: auto foo() { …
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1
vote
1 answer

Right values as function argument, correct usage?

Trying to use rightvalues a bit more but I got confused, how should I design my function in which I want to use the right value: // Pass by whatever-it's-called void RockyBalboa::DoSomething(std::string&& str){ m_fighters.push_back(str); } //…
dejoma
  • 394
  • 1
  • 6
  • 18
1
vote
2 answers

Why am I getting a const reference when I for(auto& it : myUnorderedMap) {... = std::move(it.second)}?

Minimally reproducible example cpp.sh/2nlzz : #include #include #include #include using namespace std; int main() { struct Movable { Movable() = default; Movable ( Movable && ) = default; //…
matthias_buehlmann
  • 4,641
  • 6
  • 34
  • 76
1
vote
1 answer

How to route to different implementations according to whether an object is a rvalue reference or not?

For example, I have a class called MyClass and create an instance from it: auto obj = MyClass() I have two ways to call its method. Option 1: call the method directly obj.method() Option 2: cast obj to rvalue reference first, then call the…
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
1
vote
2 answers

Casting to rvalue reference to "force" a move in a return value - clarification

Ok, I am starting to get the jist of rvalue references (I think). I have this code snippet that I was writing: #include using namespace std; std::string get_string() { std::string str{"here is your string\n"}; return std::move(str);…
code_fodder
  • 15,263
  • 17
  • 90
  • 167