Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Rvalue reference declarator: &&
In this articleHolds a reference to an rvalue expression. Syntax
RemarksRvalue references enable you to distinguish an lvalue from an rvalue. Lvalue references and rvalue references are syntactically and semantically similar, but they follow slightly different rules. For more information about lvalues and rvalues, see Lvalues and Rvalues. For more information about lvalue references, see Lvalue Reference Declarator: &. The following sections describe how rvalue references support the implementation of move semantics and perfect forwarding. Move semanticsRvalue references support the implementation of move semantics, which can significantly increase the performance of your applications. Move semantics enables you to write code that transfers resources (such as dynamically allocated memory) from one object to another. Move semantics works because it enables transfer of resources from temporary objects: ones that can't be referenced elsewhere in the program. To implement move semantics, you typically provide a move constructor, and optionally a move assignment operator ( You can also overload ordinary functions and operators to take advantage of move semantics. Visual Studio 2010 introduces move semantics into the C++ Standard Library. For example, the
Before Visual Studio 2010, each call to Move semantics also helps when the compiler can't use Return Value Optimization (RVO) or Named Return Value Optimization (NRVO). In these cases, the compiler calls the move constructor if the type defines it. To better understand move semantics, consider the example of inserting an element into a To take advantage of move semantics in the For more information about the introduction of move semantics into the C++ Standard Library in Visual Studio 2010, see C++ Standard Library. Perfect forwardingPerfect forwarding reduces the need for overloaded functions and helps avoid the forwarding problem. The forwarding problem can occur when you write a generic function that takes references as its parameters. If it passes (or forwards) these parameters to another function, for example, if it takes a parameter of type Ordinarily, to solve this problem, you must provide overloaded versions of the generic function that take both Consider the following example that declares four types,
Suppose you want to write a generic function that generates objects. The following example shows one way to write this function:
The following example shows a valid call to the
However, the following example doesn't contain a valid call to the
Ordinarily, to solve this problem, you must create an overloaded version of the
This example uses rvalue references as the parameters to the The following example
shows the
Properties of rvalue referencesYou can overload a function to take an lvalue reference and an rvalue reference. By overloading a function to take a
This example produces the following output:
In this example, the first call to The compiler treats a named rvalue reference as an lvalue and an unnamed rvalue reference as an rvalue. Functions that take an rvalue reference as a parameter treat the parameter as an lvalue in the body of the function. The compiler treats a named rvalue reference as an lvalue. It's because a named object can be referenced by several parts of a program. It's dangerous to allow multiple parts of a program to modify or remove resources from that object. For example, if multiple parts of a program try to transfer resources from the same object, only the first transfer succeeds. The following example shows the function
This example produces the following output:
In the example, the
The C++ Standard Library
This example produces the following output:
Function templates deduce their template argument types and then use reference collapsing rules. A function template that passes (or forwards) its parameters to another function is a common pattern. It's important to understand how template type deduction works for function templates that take rvalue references. If the function argument is an rvalue, the compiler deduces the
argument to be an rvalue reference. For example, assume you pass an rvalue reference to an object of type The following example
declares one structure template and then specializes it for various reference types. The
This example produces the following output:
To resolve each call to the
The compiler uses reference collapsing rules to reduce the signature:
This version of the The following table summarizes the reference collapsing rules for template argument type deduction:
Template argument deduction is an important element of implementing perfect forwarding. The Perfect forwarding section describes perfect forwarding in more detail. SummaryRvalue references distinguish lvalues from rvalues. To improve the performance of your applications, they can eliminate the need for unnecessary memory allocations and copy operations. They also enable you to write a function that accepts arbitrary arguments. That function can forward them to another function as if the other function had been called directly. See alsoExpressions with unary operators Feedback
Submit and view feedback for When a called function is not allowed to modify the parameters this semantics is known as O?When a called function is not allowed to modify the parameters, this semantics is known aspass-only.
Why can we not modify parameter variables in a function?The problem here is that if the input parameters are modified at some point in the routine, they will no longer represent their original values. Then, developers who come later may be operating under some false assumptions. This then leads to severe bugs in logic, that aren't easily caught.
Which of the following parameters is used in the function call?Answer : Parameters used in function call statement are known as actual parameter.
What are the three semantic models of parameter passing?Semantic Models of Parameter Passing
Formal parameters are characterized by one of three distinct semantic models: in mode: They can receive data from corresponding actual parameters. out mode: They can transmit data to the actual parameter. inout mode: They can do both.
|