Questions tagged [stdmove]

117 questions
1071
votes
9 answers

What is std::move(), and when should it be used and does it actually move anything?

What is it? What does it do? When should it be used? Good links are appreciated.
Basilevs
  • 22,440
  • 15
  • 57
  • 102
26
votes
5 answers

Using an object after std::move doesn't result in a compilation error

After std::move is called on an object, why doesn't the language cause a compilation error if the object is used after? Is it because it is not possible for compiler to detect this condition?
Jubin Chheda
  • 534
  • 4
  • 11
21
votes
4 answers

What lasts after using std::move c++11

After using std::move in a variable that might be a field in a class like: class A { public: vector&& stealVector() { return std::move(myVector); } void recreateMyVector() { } private: vector myVector; …
Nezquick
  • 455
  • 4
  • 11
19
votes
2 answers

Where to use std::move for strings?

I was reading this post. And I reached to the following code. I was wondering: Is std::move useful for strings (assuming the string is long enough)? Does it invalidate the previous string? Where should I use it and where I should not? . class…
ar2015
  • 5,558
  • 8
  • 53
  • 110
18
votes
1 answer

Is the front address of std::vector move invariant?

In the following snippet: std::vector a(100, 4.2); auto* a_ptr = a.data(); auto b = std::move(a); auto* b_ptr = b.data(); std::cout << ((b_ptr == a_ptr) ? "TRUE" : "FALSE") << '\n'; does the C++ standard guarantee that b_ptr is always…
Teodor Nikolov
  • 783
  • 7
  • 14
18
votes
4 answers

Should I return an rvalue reference (by std::move'ing)?

A C++Next blog post said that A compute(…) { A v; … return v; } If A has an accessible copy or move constructor, the compiler may choose to elide the copy. Otherwise, if A has a move constructor, v is moved. Otherwise, if A has a copy…
StereoMatching
  • 4,971
  • 6
  • 38
  • 70
16
votes
1 answer

Why to use std::move despite the parameter is an r-value reference

I am confused about using std::move() in below code: If I uncomment line at (2) the output would be: 1 2 3 but if I uncomment line at (1) output would be nothing which means that move constructor of std::vector was called! Why do we have to make…
Islam Abdeen
  • 539
  • 3
  • 10
13
votes
1 answer

Why is a std::move of a value from a const std::optional possible?

Why is the following snippet legal C++ code? This is a purely theoretical question - there is no usecase that I have in mind: #include #include #include int main() { std::vector v{1, 2, 3}; const…
Markus Moll
  • 163
  • 8
12
votes
1 answer

Why doesn't C++ move construct rvalue references by default?

Say I have the following function void doWork(Widget && param) // param is an LVALUE of RRef type { Widget store = std::move(param); } Why do I need to cast param back to an rvalue with std::move()? Shouldn't it be obvious that the type of…
barney
  • 2,172
  • 1
  • 16
  • 25
11
votes
2 answers

Could it be possible to have types with move operations that throw in containers?

While explaining move operations on objects with a colleague, I basically said that move operations should not throw exceptions in a container because if the move operation fails, then there is no way to bring back the original object reliably. …
Adrian
  • 10,246
  • 4
  • 44
  • 110
10
votes
1 answer

Why is my code printing rvalue 2 times instead of rvalue & lvalue?

So I wanted to practice the usage of std::forward and created a Test class with 2 constructors. 1 with T& and the other with T&& as overload. T& prints lvalue, and T&& prints rvalue so I know which one of the constructors is being used. I create 2…
3l4x
  • 103
  • 4
10
votes
3 answers

Using std::move to pass in a temp lambda, or to "pull" out a temp parameter and what is the difference?

I have the following (contrived) code where I have a printer class with a single print function in it and a working class that processes a string and then calls a callback function to the print function: #include #include…
code_fodder
  • 15,263
  • 17
  • 90
  • 167
7
votes
0 answers

why I can't bind function with rvalue args like this?

#include #include #include #include using namespace std; template void do_task(Func &func, Args &&...args) { auto f = bind(func, forward(args)...); …
Xuan Lin
  • 81
  • 3
6
votes
3 answers

Assign std::vector to std::vector WITHOUT copying memory

I have a function that returns a std::vector I am aware that std::byte is not a character type nor an integral type, and that converting it to char is only possible through a typecast. So far so good. So I would like (in cases where I…
matt
  • 95
  • 5
6
votes
1 answer

Why does the std::move() work in c++?

Following is the code snippet: int i=0; int&&k=std::move(i); In c++ primer the move is template typename remove_reference::type &&move(T&& t) {return static_cast::type&&>(t);} As far as i know,this…
user9396941
1
2 3 4 5 6 7 8