C ++ interface object parameter transfer design, interface parameters
When designing an interface, if you need to pass an object, and then save it.
Make it accept the left value and the right value
Explicit A (string & a): _ a (move (a) {} explicit A (const string & a): _ a (){}
The above move construct, the next copy construct, but writing two is too troublesome
2. Use perfect forwarding
Template <typename T> explicit A (T & a): _ a (forward ()){}
In this way, only the header file can be written, and various strange types can be passed in. In addition, perfect forwarding cannot be used in some cases, for example:
Braced initializers, 0 or NULL, only the declared number const static member, with overload or template function as the parameter, bitfields
3. directly pass by value, and then use move in it
Explicit A (string a): _ a (move ()){}
In this way, the right value is a move construct, the right value is a move construct, the left value is a copy construct, and a move construct, the calculation is only one more move construct, but this overhead is very low, so the conclusion is that we should write it like this.
In addition, if the upper layer is const & and so on, move cannot be converted to the right value.
Void test (const string & a) {A B (move (a); // or left value reference, because &}