Topic: `X: : X(X&)' makes `X foo(); X x(foo())' illegal?
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sun, 19 Jun 1994 08:05:42 GMT Raw View
jason@cygnus.com (Jason Merrill) writes:
>>>>>> Robert Schmidt <robert@idt.unit.no> writes:
>
>> No, compilers need not report this as an error. The lifetime of a
>> temporary has been the cause of much discussion -
>
>Lifetime of temporaries isn't the issue here; the question is whether or
>not you can bind a non-const reference to a temporary, and the answer is
>currently no, since the temporary is not an lvalue.
OK, so answers my first question - the program is illegal.
(Someone else noted that there is at least one compiler which rejects it.)
Now what about the second - what happens if you replace
X x(foo());
with
X x = foo();
?
Now the compiler is allowed to eliminate the temporary - does that
mean that it doesn't have to report an error? Or must it do
full error-checking *before* attempting to eliminate temporaries?
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
Author: jason@cygnus.com (Jason Merrill)
Date: Sun, 19 Jun 1994 09:36:19 GMT Raw View
>>>>> Fergus Henderson <fjh@munta.cs.mu.OZ.AU> writes:
> Now what about the second - what happens if you replace
> X x(foo());
> with
> X x = foo();
> ?
> Now the compiler is allowed to eliminate the temporary - does that
> mean that it doesn't have to report an error? Or must it do
> full error-checking *before* attempting to eliminate temporaries?
I would argue that since elimination of temporaries falls under "as-if", it
cannot be done if doing so would change the semantics.
Reading the standard like this leads me to conclude that you cannot
initialize an object with a copy constructor that takes a non-const
reference from a function returning by value. Or, for that matter, from an
explicit constructor call, as in the common example
complex c = complex (1, 2);
since an explicit constructor call returns a temporary.
Jason
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Fri, 17 Jun 1994 03:24:26 GMT Raw View
fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:
>Is the following program strictly conforming to the ARM and/or latest
>working paper?
>
> struct X {
> X(X&) {}
Oops. Of course I also want:
X() {}
> };
> X foo() { X x; return x; }
>
> int main() {
> X x(foo());
> }
--
Fergus Henderson - fjh@munta.cs.mu.oz.au