Topic: [Q} Rationale behind C++ rule on copying temporary object to const reference
Author: Michiel.Salters@logicacmg.com (Michiel Salters)
Date: Mon, 25 Oct 2004 16:11:30 GMT Raw View
loose@astron.nl ("Marcel Loose") wrote in message news:<2t6r1kF1sk6l5U1@uni-berlin.de>...
> Hi,
>
> Can anyone explain the rationale behind the C++ standard rule
> [dcl.init.ref]/5, bullet 2, sub-bullet 1 (see also [class.temporary]/2) ?
> In short, what this rule says is that, when you pass a temporary object to a
> (member) function that expects a const reference, the temporary should be
> copy-constructible. To me, this is counter-intuitive. One of the reasons (I
> was taught) to use const references in a function's parameter list is to
> allow passing of temporary objects, because temporary objects are
> effectively const. This rule obviously forbids this use for non-copyable
> objects.
Th rationale was that there may be compilers which cannot implement
direct binding. However, during the discussion of CWG issue 391 this
was reconsidered, and it seems there is no fundamental prolem. Based
on this, there is consensus to drop the freedom of compilers here,
and to require direct binding.
(Note: the wording in some versions of issue said:
"Notes from the March 2004 meeting:
After discussing issue 450, we found ourselves reconsidering this, and we
are not inclined to make a change to require the direct binding in all
cases, with no restriction on long-lived references. Note that such a
change would eliminate the need for a change for issue 291."
This is incorrect; we _are_ inclined to make a change. )
> So, if I understand it correctly, if I have a class that is non-copyable, I
> am you are forced to first create an instance of that class and then pass
> that instance to the function which may create a copy of that instance. What
> a waste of CPU cycles!. I don't get it. Anyone, please?
Theoretically, a compiler could waste CPU cycles on anything. In practice,
compilers didn't copy anyway. This was basically a QoI issue.
Regards,
Michiel Salters
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: loose@astron.nl ("Marcel Loose")
Date: Thu, 14 Oct 2004 20:25:02 GMT Raw View
Hi,
Can anyone explain the rationale behind the C++ standard rule
[dcl.init.ref]/5, bullet 2, sub-bullet 1 (see also [class.temporary]/2) ?
In short, what this rule says is that, when you pass a temporary object to a
(member) function that expects a const reference, the temporary should be
copy-constructible. To me, this is counter-intuitive. One of the reasons (I
was taught) to use const references in a function's parameter list is to
allow passing of temporary objects, because temporary objects are
effectively const. This rule obviously forbids this use for non-copyable
objects.
So, if I understand it correctly, if I have a class that is non-copyable, I
am you are forced to first create an instance of that class and then pass
that instance to the function which may create a copy of that instance. What
a waste of CPU cycles!. I don't get it. Anyone, please?
Regards,
Marcel Loose.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]