|
Boost : |
From: Raoul Gough (RaoulGough_at_[hidden])
Date: 2004-02-07 05:13:23
David Abrahams <dave_at_[hidden]> writes:
[snip]
> 12.2/1 says that the act of binding an rvalue to a (const) reference
> creates a temporary (which may be elided). The creation of the
> temporary requires the existence of a copy ctor with a const&
> argument. As far as I know, that rule serves no useful purpose, but I
> may have missed something.
I seem to remember a supposed useful purpose in cases where small
objects can be passed in registers. I can never quite convince myself
one way or the other on this point, but it seems like giving up
language power in favour of the a hypothetical performance
optimization. I'm sure this rule confounded some move-semantics
proposal from Andrei Alexandrescu as well.
> *If* my example actually works other than this case, I'd like to try
> to get the rule lifted, and soon!
Isn't 8.5.3 the really hard part? I once discussed this in some depth
with Rani Sharoni, and ended up posting a proposal to comp.std.c++
about relaxing the copy constructor requirement for const& function
arguments. Let's just say that it wasn't met with much interest! The
argument was basically that the compiler can be forced *not* to copy
the rvalue anyway, so there can't be a fundamental reason to require
an accessible copy constructor.
e.g.
struct A { A &as_lvalue () { return *this; } };
void foo (A const &);
void bar () {
foo (A()); // Can copy
foo (A().as_lvalue()); // Can't copy
}
I'd still like to see the requirement disappear (many compilers don't
enforce it anyway, and never actually create the temporary). The other
reference binding case is somewhat trickier, of course:
void bar() {
A const &temp1 = A(); // Can copy
A const &temp2 = A().as_lvalue(); // Dangling reference!
}
-- Raoul Gough. export LESS='-X'
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk