Questions tagged [stdmove]
117 questions
5
votes
2 answers
C++11: 'decltype class instance declaration' with std::move( ) doesn't call 'move constructor.' Why?
I recently started using c++ and I chose to learn c++11 features.
But how c++ codes run is sometimes not so tangible.
below is my code.
At the part with decltype(std::move(sample)) sample2 = std::move(sample); I'm not sure why this line doesn't…

Kiseong Yoo
- 97
- 1
- 7
4
votes
3 answers
pass rvalue to std::move and bind rvalue refence to it generate stack-use-after-scope error in ASAN
considering this code snippet
#include
int foo() {
int a;
return a;
}
int main() {
auto &&ret = std::move(foo());
std::cout << ret;
}
compile with ASAN g++ -fsanitize=address -fno-omit-frame-pointer -g -std=c++17 main.cpp -o…

codesavesworld
- 587
- 3
- 10
4
votes
1 answer
Initialization in return statements of functions that return by-value
My question originates from delving into std::move in return statements, such as in the following example:
struct A
{
A() { std::cout << "Constructed " << this << std::endl; }
A(A&&) noexcept { std::cout << "Moved " << this << std::endl; }
…

Ruperrrt
- 489
- 2
- 13
4
votes
1 answer
Passing vector of unique_ptr with move semantics in c++
I am learning CPP++14 move semantics.While writing a small code I observed some weird behavior. I am moving vector of unique ptr to a function using r-value refrence. on debuuging I found that the changes are being applied to the moved object also.…

asheesh kumar singhal
- 145
- 1
- 4
4
votes
1 answer
Passing a function identifier as an rvalue reference and applying std::move() to it
Consider the following snippet
#include
#include
using callback = std::function;
double sum (double a, double b) {
return a + b;
}
int main (int argc, char *argv[]) {
// Shouldn't this…

Acsor
- 1,011
- 2
- 13
- 26
3
votes
0 answers
Why doesn't move-assigning a std::vector seem to have any performance benefit over copying in this code?
Since move-assigning a std::vector is is a O(1) time operation and copying a std::vector to another is O(N) (where N is the sum of the sizes of the 2 vectors), I expected to see move-assignment having a significant performance advantage over…

ahskdjfk
- 919
- 5
- 8
3
votes
0 answers
Why do I need static_cast and std::remove_reference in std::move if I pass an rvalue?
I'm trying to understand more move semantic. Now this is a possible implementation of std::move:
template
typename std::remove_reference::type&& move_(T&& obj)
{
return static_cast

Maestro
- 2,512
- 9
- 24
3
votes
3 answers
std::move a variable that is going to be overwritten
I ran up on some code
template
constexpr // since C++20
T accumulate(InputIt first, InputIt last, T init)
{
for (; first != last; ++first) {
init = std::move(init) + *first; // std::move since C++20
}
…

Bob
- 1,433
- 1
- 16
- 36
3
votes
2 answers
Is it legal to move elements from standard containers?
I am writing a class which requires highly efficient function for filtering member container (lets say std::vector). This function should have interface similar to the following:
void filter(std::vector& items, const std::vector&…

Glib
- 304
- 1
- 13
3
votes
2 answers
Should I change this code that uses push_back to use std::move?
I have just read about the rvalue references and below is the code I am referring to
vector v;
string s = "hello";
v.push_back(s);
cout << s << endl;
cout << v[0] << endl;
return 0
My question is that till now, whenever I have seen…

Kraken
- 23,393
- 37
- 102
- 162
3
votes
1 answer
Why aren't C++ compilers optimizing more string constructions away in pass-by-value scenarios?
(This question is inspired by Nicolai Josuttis' CppCon 2017 talk.)
Consider the following source file (for an object, not a complete program):
#include
class C {
std::string s_;
public:
C(std::string s) : s_(s) { };
void…

einpoklum
- 118,144
- 57
- 340
- 684
2
votes
3 answers
Move semantics and std::move
I have a general question about move semantics. Yesterday I just played around to get more comfortable with this topic. Here I added copy and move constructor operators, that simply log to the console:
#include
class Test {
public:
…

Chris
- 75
- 1
- 5
2
votes
1 answer
C++ move semantics string
I am working on an embedded system and therefore it is not possible to throw exceptions, just to mention it. Therefore my return value will be a struct. I'm not sure if I have to call std::move on std::string. This below is a very reduced,…

Chris
- 75
- 1
- 5
2
votes
1 answer
Does std::move called on a prvalue deconstruct the object?
I 've wrote such code applying std::move to a prvalue from a temporary constructor.
// a std::string instance in class Obj
Obj&& myObj1 = std::move(Obj(1,"lost"));
print(myObj1);
Obj&& myObj2 = Obj(2,"keep");
print(myObj2);
And the…

Pan
- 125
- 2
- 5
2
votes
2 answers
Reuse variable after move
Is it okay to do the following? The following code is using the vector v again in the loop (next iteration) after moving it.
#include
#include
#include
using namespace std;
void test(vector&& v) {
cout <<…

M K
- 356
- 3
- 8