Move semantics is the C++11 feature that allows a copy operation to be replaced by a more efficient "move" when the source object is an rvalue (typically a temporary)
4
votes
3answers
107 views
Implementing Move Constructor by Calling Move Assignment Operator
The MSDN article, How to: Write a Move Constuctor, has the following recommendation.
If you provide both a move constructor and a move assignment operator for your class, you can eliminate ...
5
votes
3answers
905 views
Static arrays VS. dynamic arrays in C++11
I know that it's a very old debate that has already been discussed many times all over the world. But I'm currently having troubles deciding which method I should use rather than another between ...
15
votes
2answers
678 views
When are implicit move constructors not good enough?
When are implicit move constructors not good enough?
Should I treat it like destructors and copy constructors, where it's generally only necessary if I manage my own memory?
Is the implicit move ...
2
votes
2answers
146 views
std::move in constructor initializer list in class template
I have a template like this:
template<typename T>
struct foo {
T m_t;
foo(T t) : m_t(t) {}
};
The problem is that I want to support both small/regular types and huge types (like matrices) ...
3
votes
1answer
210 views
Will a vector of movable elements resize efficiently?
Let's assume T is moveable object:
vector<T> v;
v.resize(...)
if reallocation is needed, then will that code invoke copy, or move constructor on all elements?
If the answer is "move ...
3
votes
1answer
156 views
How to use c++11 move semantics to append vector contents to another vector?
Consider this snippet:
class X;
void MoveAppend(vector<X>& src, vector<X>& dst) {
dst.reserve(dst.size() + src.size());
for (const X& x : src) dst.push_back(x);
...
0
votes
2answers
52 views
C++11 - Move object containing filestream
I've got the following (simplified problem):
class Stream()
{
std::ofstream mStr;
public:
Stream() : mStr("file", ofstream::out)
{}
Stream(const Stream & rhs) = delete;
...
16
votes
2answers
210 views
Why throw local variable invokes moves constructor?
Recently, I've "played" with rvalues to understand their behavior. Most result didn't surprize me, but then I saw that if I throw a local variable, the move constructor is invoked.
Until then, I ...
65
votes
3answers
20k views
What is std::move()?
What is it?
What does it do?
When should it be used?
Good links are appreciated.
15
votes
6answers
507 views
Are C++11 move semantics doing something new, or just making semantics clearer?
I am basically trying to figure out, is the whole "move semantics" concept something brand new, or it is just making existing code simpler to implement? I am always interested in reducing the number ...
3
votes
3answers
109 views
C++ move semantics- what exactly is it to achieve? [duplicate]
What exactly is the purpose of this "move" semantic? I understand if you don't pass in by reference a copy is made of non-primitive types, but how does "move" change anything? Why would we want to ...
1
vote
3answers
72 views
Concatenating two moved strings
The code below:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "hello";
string s2 = "my";
string s3 = "world";
string s4;
s4 = ...
1
vote
2answers
50 views
Why is using move semantics in this way invalid?
I was tracking down a compilation error when I came to this case:
struct Y
{
int&& y;
Y(int&& y)
: y(y)
{
} ...
42
votes
4answers
3k views
Why do we copy then move?
I saw code somewhere in which someone decided to copy an object and subsequently move it to a data member of a class. This left me in confusion in that I thought the whole point of moving was to avoid ...
3
votes
3answers
134 views
Move construction from const reference
I have the following situation where I need to move construct t2 from t1.
Unfortunately it is not possible to do that (constness violation I suppose)
What is the right approach to handle that ...