Reputation: 9753
The first example that take A by value does two moves and the one by refref only does one move. What is the difference?
struct A
{
A() { cout << "constructor" << endl;}
A(const A&) { cout << "copy constructor " << endl;}
void operator=(const A&) { cout << "assignment operator" << endl; }
A( A&&) { cout << "move copy constructor" << endl;}
void operator=(A&&) { cout << "move assignment operator" << endl;}
};
struct C {
void func(A t) {
d.a = std::move(t);
}
struct Data {
A a;
};
Data d;
};
struct B {
void func(A t) {
C c;
c.func(std::move(t));
}
};
//////////////////////////////////////////////////////////
struct C {
template<class T>
void func(T&& t) {
d.a = std::forward<T>(t);
}
struct Data {
A a;
};
Data d;
};
struct B {
template<class T>
void func(T&& t) {
C c;
c.func(std::forward<T>(t));
}
};
Upvotes: 1
Views: 128
Reputation: 4084
From cppreference.com:
When used according to the following recipe in a function template, forwards the argument to another function exactly as it was passed to the calling function.
template<typename T> wrapper(T&& arg) { foo(std::forward<T>(arg)); }
So in your snippet
struct B {
template<class T>
void func(T&& t) {
C c;
c.func(std::forward<T>(t));
}
};
The std::foward<T>(t)
will simply forward your T&&
object to c.func()
exactly as B::func()
was called. This doesn't require a move, which is why you are seeing fewer moves using std::forward<T>
.
I would really recommend checking out Scott Meyer's blog post on this topic of std::move
and std::forward
: http://scottmeyers.blogspot.com/2012/11/on-superfluousness-of-stdmove.html
Upvotes: 2