C ++ interface object parameter transfer design, interface parameters

Source: Internet
Author: User

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 &}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.