 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
tpochep@mail.ru Guest
|
Posted: Fri Jun 17, 2005 10:23 am Post subject: implicitly-defined copy ctor |
|
|
Hello.
struct A
{
A(int & r) : ri_(r){}
int & ri_;
};
int main()
{
int i = 0;
A a(i);
A b(a);
}
Clause 12 (12.8/ says:
The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects. The
order of copying is the same as the order of initialization of bases
and members in a user-defined constructor
(see 12.6.2). Each subobject is copied in the manner appropriate to its
type:
- if the subobject is of class type, the copy constructor for the
class is used;
- if the subobject is an array, each element is copied, in the manner
appropriate to the element type;
- if the subobject is of scalar type, the built-in assignment
operator is used.
Virtual base class subobjects shall be copied only once by the
implicitly-defined copy constructor (see
12.6.2).
What about references?
AFAIK references are not scalar types. Should I explicitly declare copy
ctor? Or compiler-generated is everywhere (everywhere == all
implementations) good?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sat Jun 18, 2005 9:30 am Post subject: Re: implicitly-defined copy ctor |
|
|
<tpochep (AT) mail (DOT) ru> wrote:
<snip>
| Quote: | Clause 12 (12.8/ says:
The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects. The
order of copying is the same as the order of initialization of bases
and members in a user-defined constructor
(see 12.6.2). Each subobject is copied in the manner appropriate to its
type:
- if the subobject is of class type, the copy constructor for the
class is used;
- if the subobject is an array, each element is copied, in the manner
appropriate to the element type;
- if the subobject is of scalar type, the built-in assignment
operator is used.
Virtual base class subobjects shall be copied only once by the
implicitly-defined copy constructor (see
12.6.2).
What about references?
AFAIK references are not scalar types.
|
Nor are they objects!
| Quote: | Should I explicitly declare copy ctor? Or compiler-generated is
everywhere (everywhere == all implementations) good?
|
The implicitly-defined copy constructor will copy members of reference
type along with the other members. I'm not aware of any
implementations that fail to do this.
--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
tpochep@mail.ru Guest
|
Posted: Mon Jun 20, 2005 9:16 am Post subject: Re: implicitly-defined copy ctor |
|
|
Hello.
Ben Hutchings пиÑ?аЛ(а):
| Quote: | Nor are they objects!
|
Yes, I know.
| Quote: | The implicitly-defined copy constructor will copy members of reference
type along with the other members. I'm not aware of any
implementations that fail to do this.
|
Yes, all my compilers do the same, but is this behaviour defined by
standard?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Jun 20, 2005 3:34 pm Post subject: Defect report: handling of reference members not specified i |
|
|
Ben Hutchings wrote:
| Quote: | tpochep (AT) mail (DOT) ru> wrote:
snip
Clause 12 (12.8/ says:
The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects. The
order of copying is the same as the order of initialization of bases
and members in a user-defined constructor
(see 12.6.2). Each subobject is copied in the manner appropriate to its
type:
- if the subobject is of class type, the copy constructor for the
class is used;
- if the subobject is an array, each element is copied, in the manner
appropriate to the element type;
- if the subobject is of scalar type, the built-in assignment
operator is used.
Virtual base class subobjects shall be copied only once by the
implicitly-defined copy constructor (see
12.6.2).
What about references?
AFAIK references are not scalar types.
Nor are they objects!
|
Nor are they mentionned in the above paragraph.
| Quote: | Should I explicitly declare copy ctor? Or compiler-generated
is everywhere (everywhere == all implementations) good?
The implicitly-defined copy constructor will copy members of
reference type along with the other members. I'm not aware of
any implementations that fail to do this.
|
I think the original poster has a point. All of the
implementations I know do copy initialize member references, and
I can't imagine that anything else was intended. But what the
standard actually says is that "the implicitly-defined copy
constructor for class X performs a memberwise copy of its
subobjects." References aren't objects; can they be subobjects?
If not, then the standard doesn't say what an implementation
should do with them, and when the standard doesn't specify
something, it's undefined behavior.
I suspect that this is a defect in the standard. Minor, since
we all know what is wanted (and all implementations seem to
agree with us), but technically a defect. The first sentence
should probably read "[...] a memberwise copy of its subobjects
and members of reference type," and there should be an
additional point for references.
I've added comp.std.c++ to the groups.
--
James Kanze GABI Software
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[ 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 ]
|
|
| Back to top |
|
 |
tony_in_da_uk@yahoo.co.uk Guest
|
Posted: Tue Jun 21, 2005 4:12 pm Post subject: Re: implicitly-defined copy ctor |
|
|
References are little different to pointers in terms of storage and
copying issues - basically a plain-old-data (POD) type storing a number
that happens to be a memory address. In the list above I'm sure
they're both intended to be covered by scalar types, and IMHO the
standard's wording is fine. Subobjects is obviously used in a general
english sense, and defined by the list provided in the quoted section
of the standard.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|