Questions tagged [move]

Usually refers to move semantics; consider using that tag instead. Move semantics is a programming language feature that allows a copy operation to be replaced by a more efficient "move" when the source object is a temporary or an otherwise expiring object.

Usually refers to ; consider using that tag instead.

Move semantics is a programming language feature that allows a copy operation to be replaced by a more efficient "move" when the source object is a temporary or an otherwise expiring object.

For more information on move semantics in C++, see Rvalue references and move constructors.

Related tags are , , and .

3461 questions
28
votes
2 answers

Am I guaranteed that pointers to std::vector elements are valid after the vector is moved?

Considering this example: std::vector v1 = { 1, 2, 3 }; const int* i = &v1[1]; std::vector v2(std::move(v1)); std::cout << *i << std::endl; Even though in many STL implementations this will probably work, am I guaranteed by the standard…
gd1
  • 11,300
  • 7
  • 49
  • 88
27
votes
2 answers

"move" two vectors together

If I have two vectors and want to combine them to one, I can do it the following way: std::vector a(100); // just some random size here std::vector b(100); a.insert(std::end(a), std::begin(b), std::end(b)); That involves copying though,…
Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
26
votes
4 answers

Use of std::move in std::accumulate

In my Fedora 34 environment (g++), std::accumulate is defined as: template constexpr inline T accumulate(ITER first, ITER last, T init) { for (; first != last; ++first) init = std::move(init) + *first; // why move…
curiousguy12
  • 1,741
  • 1
  • 10
  • 15
26
votes
2 answers

Is move constructor called twice in C++?

Look at this code: class Foo { public: string name; Foo(string n) : name{n} { cout << "CTOR (" << name << ")" << endl; } Foo(Foo&& moved) { cout << "MOVE CTOR (moving " << moved.name << " into -> " << name…
gedamial
  • 1,498
  • 1
  • 15
  • 30
25
votes
1 answer

Which rules determine whether an object is trivially copyable

With the introduction of c++11, trivially copyableness has gotten quite relevant. Most notably in the use of 'std::atomic'. The basics are quite simple. A class foo is trivially copyable if: foo* src = new foo(); foo* dest =…
laurisvr
  • 2,724
  • 6
  • 25
  • 44
25
votes
6 answers

Move files in C#

I am moving some images (filenames are(1).PNG, (2).PNG and so on) from one directory to another. I am using the following code: for (int i = 1; i < n; i++) { try { from = "E:\\vid\\(" + i + ").PNG"; to =…
MKS
  • 736
  • 2
  • 14
  • 33
24
votes
2 answers

Repeated std::move on an boost::asio socket object in C++11

I am exploring using boost::asio along with C++11 features. In particular, I am focusing on an example called "async_tcp_echo_server.cpp", located here (code is also shown at the end of my…
24
votes
3 answers

Batch file to move files to another directory

I hope that you can help me with this one. It might have been asked multiple times already (I know that), but for some reason, I just can't have it working. I want to move some files from the "files" directory to the root directory. So the files…
user2077474
  • 353
  • 1
  • 6
  • 15
23
votes
2 answers

Have you ever got this message when moving a file? mv: will not overwrite just-created

I have a bourne shell script which performs several tasks. One of these tasks is to move some files to certain directory. Today, when I ran the script I got the following message: mv: will not overwrite just-created with…
Nacho Mezzadra
  • 886
  • 2
  • 12
  • 14
23
votes
2 answers

How to move HTML element

How to move HTML element to another element. Note that, I don't mean moving element's position. Consider this HTML code:
I want to move "to_be_moved" to "target" so "target" has child…
iroel
  • 1,760
  • 1
  • 18
  • 27
23
votes
3 answers

How do I fix the path of my local git repo after move?

How do I fix the path of my local git repo after move? previous local location: /C/website new local location: /C/Projects/website remote location: git@bitbucket.org:username/website.git I moved my git repository from one folder /website to another…
Whitecat
  • 3,882
  • 7
  • 48
  • 78
23
votes
4 answers

Using gitk to view the full history of a moved file

After much searching, I have not found a satisfactory method that is easy to use to view the complete history of a moved file in Git and more importantly in Gitk. Using git log --follow [filePath] and even gitk --follow [filePath] gives you the…
22
votes
4 answers

Why does destructor disable generation of implicit move methods?

I was trying to understand what the rule of zero says by reading this blog. IMO, it says if you declare your own destructor then don't forget to make the move constructor and move assignment as default. Example: class Widget { public: ~Widget(); …
RaGa__M
  • 2,550
  • 1
  • 23
  • 44
21
votes
1 answer

How do global consts that are not copy or clone work in Rust?

Say I have the following snippet (playground) struct A { pub val: u32 } const GLOBAL_A: A = A {val: 2}; fn main() { let some_a: A = GLOBAL_A; let other_a: A = GLOBAL_A; println!("double val = {}", some_a.val +…
21
votes
1 answer

Does std::string move constructor actually move?

So here i got a small test program: #include #include #include #include class Test { public: Test(const std::vector& a_, const std::string& b_) : a(std::move(a_)), b(std::move(b_)), …
toozyfuzzy
  • 1,080
  • 1
  • 9
  • 20