Questions tagged [move-semantics]

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.

Use this tag for questions about move semantics, move constructors and move assignment.

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 .

2073 questions
1
vote
0 answers

Confusion about copy constructor vs. move constructor being called when std::vector is resized

I have the following situation: A tuple containing a custom class is stored in a vector. struct A { string m_name; void cp(const A& x) { m_name = x.m_name; cout << m_name << ": copy "; } void mv(A&& x) { m_name…
j00hi
  • 5,420
  • 3
  • 45
  • 82
1
vote
1 answer

What's the value of rvalues?

That is, how much performance improvement one typically gets in an otherwise well-designed C++03 code when one takes advantage of C++11 typename && features such as move constructors, etc.? How often it's worth the trouble, and in what…
Michael
  • 5,775
  • 2
  • 34
  • 53
1
vote
1 answer

C++ why does empty set::emplace() inserts an element into a set of pointers?

Consider the following code: struct A{}; int main() { std::set aset; aset.emplace(); std::cout << aset.size() << std::endl; //prints "1" return 0; } DEMO Why does the empty emplace() adds an element to the set of pointers?
davidhigh
  • 14,652
  • 2
  • 44
  • 75
1
vote
3 answers

Return STL objects from function without triggering move

Suppose there is a function that returns any local object, which implements move semantics, e.g. any STL container such as std::vector, std::string, etc. E.g.: std::vector return_vector(void) { std::vector tmp {1,2,3,4,5}; return…
Oleg Shirokikh
  • 3,447
  • 4
  • 33
  • 61
1
vote
1 answer

Do I use the move semantic correctly? What would be the benefit?

I wonder if I use the move semantic correctly: class Vertex{ protected: Common::Point3D position; Common::Point3D normal; Common::Point2D uv; Common::Point2D tangent; public: Vertex(Common::Point3D…
PolGraphic
  • 3,233
  • 11
  • 51
  • 108
1
vote
1 answer

Boost.Asio TCP moved-to socket destructor not enough to cleanly close?

Consider this test program : #include #include #include #include static void callback (boost::asio::ip::tcp::socket && socket) { //boost::asio::ip::tcp::socket…
Chnossos
  • 9,971
  • 4
  • 28
  • 40
1
vote
3 answers

Move semantics from one type to another using templates

Is it possible to have a struct A with a move constructor for itself and some constructor which can move from other types (e.g struct B ) but with having a template deduction going on such that the type B is not hardcoded directly as another move…
Gabriel
  • 8,990
  • 6
  • 57
  • 101
1
vote
0 answers

Statement grouping and ordering for move vs copy constructors/assignment

I have a class A with move constructor, assignment operator, copy constructor, assignment operator. The class uses (non-friend, non-member) template const A operator*(A lhs, const A& rhs) { lhs *= rhs; return…
chrisb2244
  • 2,940
  • 22
  • 44
1
vote
2 answers

How to avoid move elision when passing a temporary by value?

In the following code: Widget makeWidget() { return Widget(); } void foo(Widget widget) { ... } foo(makeWidget()); the Widget object will be always constructed in-place (inside foo function), so no move construction takes place (at least…
rubix_addict
  • 1,811
  • 13
  • 27
1
vote
4 answers

Destructor gets called before member function when assigning unique_ptr to a reference

I'm using unique_ptr, and I'm getting some odd results. This is the code: class Sniffer { public: Sniffer() { cout << "Sniffer()" << endl; s = "String!"; } void operator()() { cout << "operator()(): " <<…
1
vote
1 answer

Compiler not using move c'tor / assignment oper?

Simple question really. What is going on in the following example code that causes it not to compile? The error occurs at the first line of main(): "Use of deleted function 'std::__atomic0::...__atomic_base(...)')" #include #include…
AndyMcoy
  • 179
  • 1
  • 3
  • 13
1
vote
1 answer

How to know or test if a given type is going to be moved

I'm not looking for a type trait for movable types, nor rules for automatic generation of move operations. What I'm looking for is a general guide to know if a given type is going to be moved or copied, or a way to figure it myself with testing. In…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
1
vote
0 answers

C++11: Algorithm & data structure separation

I have the following basic class structure: class Distance : public Base { public: using Base::Base; void run(int u, int v); // indices for nodes in graph void runAll(); }; and class Base { protected: const Graph& G; Matrix&…
xZA
  • 153
  • 1
  • 7
1
vote
2 answers

Should std::function::operator bool return false after move?

In C++11, std::function is MoveConstructible, i.e. one can meaningfully invoke std::move on such objects or store them in moveable types. A quandary: what should the following code print? #include #include #include…
Nate R.
  • 141
  • 4
1
vote
1 answer

inline void addTask(Task task) vs inline void addTask(const Task &task)

I used to pass every complex structure by const & or at least by &. But with the new std::move semantic and all the optimizations that compilers offer today, is it still the option to go? Consider such example: struct Task{ unsigned timeMS; …
PolGraphic
  • 3,233
  • 11
  • 51
  • 108
1 2 3
99
100