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
1 answer

std::forward with templated overloaded function

I've got no idea why compiler gives me warnings about template instantiations. Thats a piece of code which runs just fine and outputs lvalue/rvalue properly: //template void overloaded(const /*T*/std::string& in) { std::cout <<…
Pancake
  • 107
  • 9
1
vote
1 answer

Replace std::vector's buffer with a malloc'ed char array

I have the following class holding an std::vector as a resizable "buffer" for data. class packet { public: struct PacketHeader { size_t dataSize = 0; size_t paddedDataSize = 0; } header_; const unsigned char* data()…
netik
  • 1,736
  • 4
  • 22
  • 45
1
vote
1 answer

non-generated special member functions vs deleted special member functions

This compiles and calls the copy constructor: struct foo { foo() = default; foo(const foo&) { cout << "copy ctor!" << endl; } //foo(const foo&&) = delete; }; int main() { foo a; foo b(move(a)); This does not compile: struct foo…
onqtam
  • 4,356
  • 2
  • 28
  • 50
1
vote
1 answer

move object into foo(Bar&&) function

I have a class Bar which implements a move constructor. For an instance Bar b, if I call void fooRvalRef(Bar&&) using fooRvalRef(std::move(b)) then the move constructor of Bar is not called. On the other hand for the function void foo(Bar),…
user695652
  • 4,105
  • 7
  • 40
  • 58
1
vote
3 answers

Is the last appearance of a local variable treated as an rvalue in C++11?

Are local variables' last appearance in their scope treated as an rvalue? (Like in return statements.) Eg. in the code below, are the strings moved into the vector or V.push_back(std::move(newitem)) should be written explicitly ? struct…
ovga
  • 75
  • 1
  • 5
1
vote
1 answer

Assignment from rvalue allowed when assignment operator explicitly deleted?

Consider the following code, which compiles under Clang, GCC, and VS 2015 (online example): #include class S { public: S(int x) : i(x) { } ~S() { } S(S&&) = default; S(const S& ) = delete; S&…
1
vote
3 answers

Shorthand for std::move

Would it be a bad practice for some class S to overload unary operator + (or maybe operator * for non-pointer-like classes) as following? struct S { S && operator + () & noexcept { return std::move(*this); } }; The goal it to invent shorthand for…
1
vote
1 answer

Combine Template for Perfect Forwarding and Tempate for arbitrary value_type

I have this function template foo that takes any STL container that contains int: template ::value, int> = 0> void foo(ContainerType const& list) { /* */…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
1
vote
1 answer

Are temporary lvalues a use case for std::move

I have a class like this: class Widget { public: Widget(A const& a) { /* do something */ } Widget(A&& a) { /* do something */ } }; Which I can use like this: A a{}; Widget widget{a}; If I came to the conclusion that I don't need a any…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
1
vote
1 answer

C++ Move Constructor argument

How should I interpret the argument to a move constructor or move assignment operator? The syntax is typename && name. (eg; myclass && inst) Is this a "reference to a reference", ie; works like a pointer to a pointer after compilation? Or should…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
1
vote
1 answer

Set iterator invalidated by move semantic

I have the following immutable container class (public access to values is just for reasons of simplicity): struct Container { std::unordered_set values; //Default constructor Container() = default; //Copy constructor …
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
1
vote
2 answers

How do you instantiate smart pointer class members?

I've been doing some really dodgy smart pointer programming lately because I've never really understood move constructors, move assignments etc. because the examples given on places like MSDN are really convoluted for me. By dodgy, I mean literally…
user3530525
  • 691
  • 3
  • 8
  • 20
1
vote
2 answers

About std::move behavior

I played a little with std::move and I noticed something strange: string && foo(string && x) { string && a = move(x); return move(a); //both x and a = "asdfgh" } int main(){ string x,aa; aa = "asdfgh"; x = foo(move(aa)); …
shinzou
  • 5,850
  • 10
  • 60
  • 124
1
vote
1 answer

std::make_shared and std::forward — what's the point?

I'm trying to understand why std::make_shared is declared/implemented the way it is: template inline _LIBCPP_INLINE_VISIBILITY typename enable_if < !is_array<_Tp>::value, shared_ptr<_Tp> >::type make_shared(_Args&&…
tonytony
  • 1,994
  • 3
  • 20
  • 27
1
vote
1 answer

Both move assignment and move constructor are emitted from a function call

I am new to C++11 and found move semantics and copy ellision are really great to write elegant and efficient code. However I have some problems would like to ask. Here I write a template class matrix.hpp and use it to test behaviors of move…
pateheo
  • 430
  • 1
  • 5
  • 13