Re: Does cast to its own type create a temporary copy?
Niels Dekker - no return address wrote:
Does static_cast<int>(argc) create a temporary copy of argc, in the
following
little program?
////////////////////////////////////////
#include <cassert>
void Inc(const int& input, int& output)
{
output = input + 1;
assert( output == (input + 1) );
}
int main(int argc, char**)
{
Inc( static_cast<int>(argc), argc );
}
////////////////////////////////////////
5.2.9/1:
| The result of the expression static_cast<T>(v) is the result of
| converting the expression v to type T. If T is a reference type,
| the result is an lvalue; otherwise, the result is an rvalue.
| Types shall not be defined in a static_cast. The static_cast
| operator shall not cast away constness (5.2.11)."
Here it says the result is an rvalue for static_cast<int>. Furthermore,
5.2.9/2:
| An expression e can be explicitly converted to a type T using a
| static_cast of the form static_cast<T>(e) if the declaration ?T t(e);?
| is well-formed, for some invented temporary variable t (8.5).
| The effect of such an explicit conversion is the same as performing
| the declaration and initialization and then using the temporary
| variable as the result of the conversion. The result is an lvalue
| if T is a reference type (8.3.2), and an rvalue otherwise. The
| expression e is used as an lvalue if and only if the initialization
| uses it as an lvalue.
Here it says explicitly that a temporary variable is created and used.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]