Generic Programming-Transfer constructors (Generic Programming:move constructor)

Source: Internet
Author: User
Tags constructor readfile

1 Introduction

I'm sure everyone knows that creating, copying, and destroying temporary objects is a favorite indoor sport of C + + compilers. Unfortunately, these behaviors can degrade the performance of C + + programs. Indeed, temporary objects are often considered the first factor in the inefficiency of C + + programs [1].

The following code is correct:

vector < string > ReadFile();
vector < string > vec = ReadFile();

Or

string s1, s2, s3;
//...
s1 = s2 + s3;

However, if you care about efficiency, you need to limit the use of similar code. Temporary objects created by ReadFile () and operator+ are copied and then discarded. It's a waste!

In order to solve this problem, we need some less elegant conventions. For example, you can pass a function argument by reference:

void ReadFile(vector < string > & dest);
vector < string > dest;
ReadFile(dest);

It's pretty annoying. Worse, operators do not have this option, so if you want to efficiently handle large objects, programmers must limit the use of the operators that create temporary objects:

string s1, s2, s3;
//...
s1 = s2;
s1 += s3;

This difficult approach often slows down the productivity of large teams that design large programs, and this imposed constant annoyance kills the fun of coding and increases the number of code. Is it wrong to return a value from a function and pass a temporary object with an operator?

A proposal for a formal language based solution has been submitted to the standardization Committee [2]. Usenet has already sparked a big discussion, and this article has been repeatedly discussed in it.

This article shows you how to troubleshoot unnecessary replication problems with C + +. There is not a completely satisfying solution, but a clean degree is achievable. Let's build a powerful framework step-by-step to help us eliminate unwanted temporary object duplication from our programs. This solution is not completely transparent, but it eliminates all unwanted replication and is encapsulated enough to provide a reliable alternative until years later, a clean, standards-based implementation of the language is present.

Related Article

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.