Are the move semantics used in Example A necessary, and which struct is superior?
Example A:
struct A
{
std::string a;
A( std::string a ) : a( std::move(a) ){ }
};
Example B:
struct B
{
std::string b;
B( const std::string& b ) : b( b ){ }
};
I don't believe this is a duplicate question. I am asking specifically which example is superior from the perspective of using member initialization in a class constructor. None of the examples or answers listed in the other question dealt with member initialization.
I don't like that the constructor is called with a reference parameter, then copied into the member. It seems that it could be wasteful to have multiple copy operations.
I want to "pipe" the data into the members as efficiently as possible but I don't want to take rvalues as the constructor parameters.
int
or other single value primitive, using eithermove
or references (const or otherwise) is pointless. Also, I'd consider the struct without an explicit constructor superior in this case, what with aggregate initialization working just fine here. – Holmsint
for simplification of the example. If it werestd::string
or any other more complicated class, which would be superior? (edited my answer and changedint
to bestd::string
) – MadaleneA( std::string&& )
. – Devin