Is std::move really needed on initialization list of constructor for heavy members passed by value?
Posted
by
PiotrNycz
on Stack Overflow
See other posts from Stack Overflow
or by PiotrNycz
Published on 2014-05-29T09:16:38Z
Indexed on
2014/05/29
9:25 UTC
Read the original article
Hit count: 176
Recently I read an example from cppreference.../vector/emplace_back:
struct President
{
std::string name;
std::string country;
int year;
President(std::string p_name, std::string p_country, int p_year)
: name(std::move(p_name)), country(std::move(p_country)), year(p_year)
{
std::cout << "I am being constructed.\n";
}
My question: is this std::move
really needed? My point is that compiler sees that this p_name
is not used in the body of constructor, so, maybe, there is some rule to use move semantics for it by default?
That would be really annoying to add std::move on initialization list to every heavy member (like std::string
, std::vector
). Imagine hundreds of KLOC project written in C++03 - shall we add everywhere this std::move
?
This question: move-constructor-and-initialization-list answer says:
As a golden rule, whenever you take something by rvalue reference, you need to use it inside std::move, and whenever you take something by universal reference (i.e. deduced templated type with &&), you need to use it inside std::forward
But I am not sure: passing by value is rather not universal reference?
© Stack Overflow or respective owner