Topic: Defect Report - initialisation of const references and copying
Author: James Kanze <kanze@gabi-soft.de>
Date: Mon, 19 Feb 2001 21:14:38 GMT Raw View
"Anthony Williams" <anthwil@nortelnetworks.com> writes:
|> "Ron Natalie" <ron@spamcop.net> wrote in message
|> news:3A89698F.F258E926@spamcop.net...
|> > Anthony Williams wrote:
|> > > This seems to me a needless restriction, as the compiler is
|> > > required to initialise reference with the temporary copy of the
|> > > original temporary - why not just use the original temporary?
|> > I'm going out on a limb here, but one thing that comes to mind is
|> > that since the lifetime of an object bound to a const reference is
|> > extended, it may be necessary for the object to be copied to a
|> > place which will endure for the life of the reference.
|> Would it be too hard to construct the original in that enduring
|> place, as the compiler must know that the lifetime will be extended
|> - the destination location for that temporary must be decided in the
|> same statement in which it is bound to the const reference, after
|> all?
It might be, but I think the real reason was to leave open the choice of
space-time trade-off. Consider the following:
struct X {
int i ;
double d[ 100000 ] ;
} ;
X f() ;
X const& x = f() ;
int const& i = f().i ;
In the first case, I would be surprised if any compiler made a copy; the
copy was made anyway during the return, the additional copy wouldn't
save anything, and would just cost run-time. In the second case, if
memory is tight, it might be worthwhile to make a copy of the single
int, and save it, rather than keeping the large structure around.
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
---
[ 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.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Tue, 13 Feb 2001 16:11:18 GMT Raw View
8.5.3p5 [dcl.init.ref] says (among other things)
"Otherwise, the reference shall be to a non-volatile const type (i.e., cv1
shall be const).
If the initializer expression is an rvalue, with T2 a class type, and "cv1
T1" is reference-compatible with "cv2 T2," the reference is bound in one of
the following ways (the choice is implementation-defined):
* The reference is bound to the object represented by the rvalue (see 3.10)
or to a sub-object within that object.
* A temporary of type "cv1 T2" [sic] is created, and a constructor is called
to copy the entire rvalue object into the temporary. The reference is bound
to the temporary or to a sub-object within the temporary.
The constructor that would be used to make the copy shall be callable
whether or not the copy is actually done."
This means two things:
* Firstly, the initialisation of a const reference with a temporary (or
other rvalue) may or may not result in a call to the copy constructor (and
correspondingly the destructor), with any side effects that would be
associated with such a call.
* Secondly, it prevents the intialisation of a const reference with a
temporary (or other rvalue) of any class type which has an inaccessible copy
constructor.
This second point means that classes with private copy constructors cannot
serve much purpose when created as temporaries, as you cannot pass them as
arguments to functions.
This seems to me a needless restriction, as the compiler is required to
initialise reference with the temporary copy of the original temporary - why
not just use the original temporary?
I propose that the paragraph (part of 8.5.3p5 [dcl.init.ref]) quoted above
be reworded as follows:
"Otherwise, the reference shall be to a non-volatile const type (i.e., cv1
shall be const).
If the initializer expression is an rvalue, with T2 a class type, and "cv1
T1" is reference-compatible with "cv2 T2," the reference is to the object
represented by the rvalue (see 3.10) or to a sub-object within that object."
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks
The opinions expressed in this message are mine and do not represent those
of my employer
---
[ 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.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Ron Natalie <ron@spamcop.net>
Date: Tue, 13 Feb 2001 17:58:07 GMT Raw View
Anthony Williams wrote:
>
> This seems to me a needless restriction, as the compiler is required to
> initialise reference with the temporary copy of the original temporary - why
> not just use the original temporary?
I'm going out on a limb here, but one thing that comes to mind is that since
the lifetime of an object bound to a const reference is extended, it may be
necessary for the object to be copied to a place which will endure for the
life of the reference.
---
[ 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.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Thu, 15 Feb 2001 17:50:14 GMT Raw View
"Ron Natalie" <ron@spamcop.net> wrote in message
news:3A89698F.F258E926@spamcop.net...
>
>
> Anthony Williams wrote:
>
> >
> > This seems to me a needless restriction, as the compiler is required to
> > initialise reference with the temporary copy of the original temporary -
why
> > not just use the original temporary?
>
> I'm going out on a limb here, but one thing that comes to mind is that
since
> the lifetime of an object bound to a const reference is extended, it may
be
> necessary for the object to be copied to a place which will endure for the
> life of the reference.
>
Would it be too hard to construct the original in that enduring place, as
the compiler must know that the lifetime will be extended - the destination
location for that temporary must be decided in the same statement in which
it is bound to the const reference, after all?
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks
The opinions expressed in this message are mine and do not represent those
of my employer
---
[ 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.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]