If return vec;
not compiling, VS2010 does not yet support move semantics. Typically, automatic variables are moved implicitly if they are returned from a function. Use return std::move(vec);
as a temporary workaround and take a note in your head to get rid of std::move
in the future.
In this answer to frequently asked questions, a full description can be found under the heading "Moving from Functions".
In addition, your two-argument constructor creates a copy of the string argument, which is passed by a const-reference. I would suggest taking an argument by value and moving it to a member:
A(int num, std::string name) : num(num), name(std::move(name)) { }
Thus, you reduce the number of copies needed. See Need speed? Scroll by value for more information.
Also, since your move constructor does nothing special, you can default
it:
A(A&& other) = default;
This makes it more reliable in the face of change. Errors are rarely hidden in code that you don't write :)
source share