Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
1 vote
1 answer
146 views

efficient use of std::move to aggregate all object instances in a std container

I need to centrally accumulate certain entity creations in my program into a container and I want to use the efficiency of std::move with a move constructor to aggregate all entities created anwhere ...
sith's user avatar
  • 530
2 votes
3 answers
4k views

std::vector and move semantics

To enable move semantics on a std::vector do I need to pass by value; so the compiler will not copy the element but just move them? Example: class data { public: void setMyData(vector<string>...
user1583007's user avatar
12 votes
1 answer
1k views

push_back() and emplace_back() behind the scenes

I'm currently learning C++ on my own, and I am curious about how push_back() and emplace_back() work under the hood. I've always assumed that emplace_back() is faster when you are trying to construct ...
azhu5355's user avatar
  • 131
0 votes
2 answers
686 views

Difference between std::forward implementation

Recently I've been trying to understand move semantics and came up with a question. The question has already been discussed here. I implemented the first variant and checked whether it returns l-...
fminkin's user avatar
  • 162
2 votes
1 answer
734 views

why std::move behaves like std::copy? [duplicate]

Consider the following piece of code - #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> d {100, 200, 300}; std::vector<int&...
Abhinav Gauniyal's user avatar
4 votes
1 answer
156 views

C++: For which objects, being "moved" implies more than "staying valid"? [duplicate]

Given that a and b are of type T, and we have either move construction (T b(move(a));) or move assignment (b = move(a)). In what cases do we know what the value of a will be? The only one that I know ...
tohava's user avatar
  • 5,402
1 vote
1 answer
1k views

std::move items out of an STL container?

I'm trying to std::move an element out of a std::set and then erase it from the set: std::set<some_type> the_set; some_type item; for (auto iter = the_set.begin(); iter != the_set.end(); ...
atanamir's user avatar
  • 4,943
7 votes
3 answers
1k views

Why can't I std::move std::unique_ptrs between std::sets?

I really want to move some unique_ptrs from one std::set into another: #include <memory> #include <algorithm> #include <set> int main() { std::set<std::unique_ptr<int>&...
Lightness Races in Orbit's user avatar
2 votes
2 answers
860 views

Is `unique_ptr::operator bool()` defined for a unique_ptr that has been move()d from?

It is my understanding that after I have moved from a Standard Library object, that object is in a state that is valid but undefined. But in the case of a unique_ptr, just how undefined is it? ...
John Auld's user avatar
  • 486
5 votes
0 answers
147 views

Why do C++11 std containers have pass-by-ref and pass-by-rvalue insert/push methods? [duplicate]

I've seen many statements that when you want to copy a value inside a function it is better to pass it by value and do an std::move inside that function (since C++11). See here and here for example. ...
amfcosta's user avatar
  • 1,061
5 votes
3 answers
559 views

Why do standard library classes not overload swap() for rvalues?

STL classes define swap() method as void swap(A&), taking an l-value reference. See for example std::vector::swap, or the question Is it necessary to use std::move when swapping two objects? Such ...
Mikhail's user avatar
  • 21.7k
0 votes
0 answers
229 views

How to use C++ std::move on object with asio io_service and acceptor member variables

I have a software to listen on ports on three NIC, so I defined TCPServer class which uses boost examples, although I am using C++11 and ASIO. class TCPServer { private: asio::io_service& ...
Splash's user avatar
  • 1,288
2 votes
1 answer
3k views

Is it safe to reuse a std container after std::move? [duplicate]

If I have a std::list, for example, and I use std::move() to move its information into a std::map, is it safe to reuse the list by populating it with more data so that I can add more entries to the ...
Justin G's user avatar
  • 796
60 votes
2 answers
79k views

Transferring the ownership of object from one unique_ptr to another unique_ptr in C++11?

In C++11 we can transfer the ownership of an object to another unique_ptr using std::move(). After the ownership transfer, the smart pointer that ceded the ownership becomes null and get() returns ...
iampranabroy's user avatar
  • 1,796
7 votes
1 answer
729 views

Returning std::move(f) in std::for_each

I'm writing an implementation of standard c++ library for study. The C++11 standard says that for_each returns std::move(f). template <class InputIterator, class Function> Function for_each(...
Inbae Jeong's user avatar
  • 4,103
4 votes
1 answer
2k views

What can I use instead of std::move()?

I'm using a C++ compiler with the C++0x specification, and want to make my move constructor for a String class that wraps around a std::wstring. class String { public: String(String&& str)...
Mike Weir's user avatar
  • 3,191
33 votes
4 answers
15k views

Is this correct usage of C++ 'move' semantics? [duplicate]

Tonight I've been taking a look at some code I've been working on over the last few days, and began reading up on move semantics, specifically std::move. I have a few questions to ask you pros to ...
Moo-Juice's user avatar
  • 38.8k