C + + Standard: A very good reference cannot point to a temporary object (reprinted)

Source: Internet
Author: User

The C + + standard stipulates that a very good reference cannot point to a temporary object:

To prevent the assignment of constants or temporary variables (which are prone to bugs), only const references are allowed.

The following is transferred from: http://blog.csdn.net/liuxialong/article/details/6539717

To summarize:

You cannot pass a temporary object as an argument to a non-const reference.

For example:

void conv (string &str) {}int main () {    conv ("DASD");//Wrong here, the compiler automatically generates a string ("DASD") temporary object and cannot pass the temporary object to a non-const reference}

It is therefore necessary to change it to:

void conv (string str) {}//value passed

Or

void conv (const string &str) {}//const reference, because the standard specifies that temporary objects cannot be changed, so add a const modifier.

So here's a question, when will there be temporary objects?

Definition of a temporary object:

To see an example, in C + +, this TMP is a local object (variable), not a temporary object.

Template <t>void Swap (t &obj1, T &obj2) {    obj tmp=obj1;//The TMP here cannot be called a temporary object, it is only a local object    obj1=obj2;    Obj2=tmp;}

In C + +, the real temporary objects are invisible and they don't appear in your code.

Creating an unnamed non-heap (Non-heap) object produces a temporary object.

Such objects are typically produced under two conditions: implicit type conversions and function return objects for successful invocation of the function.

It is important to understand how and why these temporary objects are built, because the overhead of creating and releasing them has an undeniable impact on the performance of the program.

1, first consider the case of establishing a temporary object for a successful invocation of the function.

This occurs when the object type that is passed to the function does not match the parameter type.

For example: the example at the beginning of the article, the argument type of the CONV function is a string, but the passed-in argument is a const char *, so the compiler automatically casts it implicitly.

Generates a string ("DASD") of the temporary object, and then the object passed in, the unnamed temporary object, of course, cannot be modified, so you must add the const modifier.

This is the reason why the question at the beginning of the article arises.

2, the second environment for creating temporary objects is when a function returns an object.

For example: operator+ must return an object to represent the and of its two operands (see effective C + + clause 23).

Operator+ is stated in this way:

Template<typename t>t operator+ (const t &LHS, const T &RHS);

The return object of this function is temporary, because it is not named, it is just the return value of the function.

You have to pay for each call to operator+ to create and release this object.

A summary of the temporary objects:

Temporary objects are expensive, so you should remove them as much as possible, but it is more important to train yourself to find a place where you can create temporary objects.

1, at any time whenever you see the constant reference (reference to const) parameter, there is the possibility of establishing a temporary object and binding on the parameter.

2, at any time as soon as the return object is seen, a temporary object will be created (later released).

By learning to look for these object constructs, you can significantly enhance the ability to see the overhead behind the compiler's surface action.

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.