Topic: Defect Report: Copy-initialization in brace-enclosed initializer


Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Fri, 1 Sep 2006 06:24:06 GMT
Raw View
kanze ha scritto:
>=20
> The defect is that in =A78.5/12, the standard says explicitly that
> copy-initialization (not value-initialization or
> default-initialization) is used.  Which is a direct
> contradiction with =A78.5.1/7.
>=20

I see. It really seems to me that the whole problem is just a matter of
unfortunately chosen terms. As I see it, the terms zero-initialization,
default-initialization and value-initialization are all defined
abstractly, without references to a particular syntax, and correspond to
precise initialization algorithms described in 8.5/5. Oppositely, the
terms direct-initialization and copy-initialization name *syntaxes* and
do *not* correspond to proper initialization algorithms. They are just
used as "conditions" to make choices inside the complex algorithm
described in 8.5/14.

Maybe we should replace the terms direct-initialization and
copy-initialization with direct-initialization-syntax and
copy-initialization-syntax, respectively. I think that would clarify a
lot. In particular, it would no longer be a surprise to know that an
initializer in copy-initialization *syntax* might eventually *perform* a
value-initialization.

Does it make sense?

Ganesh

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Fri, 1 Sep 2006 18:47:29 GMT
Raw View
Francis Glassborow ha scritto:
> In article <wyJJg.89544$zy5.1495200@twister1.libero.it>, Alberto Ganesh
> Barbati <AlbertoBarbati@libero.it> writes
>> Does it make sense?
>
> Possibly but this whole area is under intense review for C++0x
>

So it's the perfect time to discuss about it, isn't it?

G.

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: jdennett@acm.org (James Dennett)
Date: Sun, 3 Sep 2006 01:41:42 GMT
Raw View
Matthias Hofmann wrote:
> "Francis Glassborow" <francis@robinton.demon.co.uk> schrieb im Newsbeitrag
> news:D6Y$QFC6uc9EFwBN@robinton.demon.co.uk...
>> In article <4ll3adF2b73pU1@individual.net>, Matthias Hofmann
>> <hofmann@anvil-soft.com> writes
>>> I do not have the C++03 draft of the standard available
>> It is not a draft, it is the current C++ Standard.
>
> It is officially called a "technical corrigendum", and a draft of the
> combined C++98 standard and corrigendum is not yet publically available. See
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/standards#14882

Unfortunately the WG21 site's details on this are out of
date, as is the FAQ for this newsgroup (the latter is something
that should be addressed shortly).

> So it seems that C++98 is still the current standard.

If you buy a current copy, you'll find you're getting
the 2003 version, which includes TC1.

A search of the ISO website for standard 14882

http://www.iso.org/iso/en/StandardsQueryFormHandler.StandardsQueryFormHandler?scope=CATALOGUE&sortOrder=ISO&committee=ALL&isoDocType=ALL&title=true&keyword=14882

gives

http://www.iso.org/iso/en/CatalogueDetailPage.CatalogueDetail?CSNUMBER=38110&ICS1=35&ICS2=60&ICS3=

which shows "Status: Published Standard" and whose
abstract starts by saying "ISO/IEC 14882:2003
specifies requirements for implementations of the
C++ programming language and standard library. By
implication, it also defines C++ programs and
their behavior."

-- James

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: eldiener_no_spam_here@earthlink.net (Edward Diener No Spam)
Date: Sun, 3 Sep 2006 03:58:45 GMT
Raw View
James Dennett wrote:
> Matthias Hofmann wrote:
>> "Francis Glassborow" <francis@robinton.demon.co.uk> schrieb im
>> Newsbeitrag news:D6Y$QFC6uc9EFwBN@robinton.demon.co.uk...
>>> In article <4ll3adF2b73pU1@individual.net>, Matthias Hofmann
>>> <hofmann@anvil-soft.com> writes
>>>> I do not have the C++03 draft of the standard available
>>> It is not a draft, it is the current C++ Standard.
>>
>> It is officially called a "technical corrigendum", and a draft of the
>> combined C++98 standard and corrigendum is not yet publically
>> available. See
>>
>> http://www.open-std.org/jtc1/sc22/wg21/docs/standards#14882
>
> Unfortunately the WG21 site's details on this are out of
> date, as is the FAQ for this newsgroup (the latter is something
> that should be addressed shortly).
>
>> So it seems that C++98 is still the current standard.
>
> If you buy a current copy, you'll find you're getting
> the 2003 version, which includes TC1.
>
> A search of the ISO website for standard 14882
>
> http://www.iso.org/iso/en/StandardsQueryFormHandler.StandardsQueryFormHandler?scope=CATALOGUE&sortOrder=ISO&committee=ALL&isoDocType=ALL&title=true&keyword=14882
>
>
> gives
>
> http://www.iso.org/iso/en/CatalogueDetailPage.CatalogueDetail?CSNUMBER=38110&ICS1=35&ICS2=60&ICS3=
>
>
> which shows "Status: Published Standard" and whose
> abstract starts by saying "ISO/IEC 14882:2003
> specifies requirements for implementations of the
> C++ programming language and standard library. By
> implication, it also defines C++ programs and
> their behavior."

The 1998 standard was downloadable for a price of $18. Is there an
equivalent deal for the 2003 standard anywhere ? Otherwise the price of
352 CHF, whatver that means, sounds outrageous.

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: stephen.clamage@sun.com (Steve Clamage)
Date: Mon, 4 Sep 2006 17:59:58 GMT
Raw View
Francis Glassborow wrote:
>> So it seems that C++98 is still the current standard.
>
>
> No, the current Standard is the 98 document + all subsequent TCs that
> have been voted out by ISO. For short hand purposes we normally refer to
> The C++ Standard (ISO/IEC 14882:1998 ) + TC 1 as C++03. By whatever name
> you like to use, that is the current C++ Standard.
>
>

The current standard is ISO/IEC 14998:2003. If you go to
http://www.iso.org
search standards for 14882, it will show you 14882:2003. The 1998
version was replaced by it.

You get the same result at ANSI.
http://www.ansi.org
click on "eStandards store", then search for 14998.

---
Steve Clamage, stephen.clamage@sun.com

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Tue, 29 Aug 2006 19:31:19 GMT
Raw View
Matthias Hofmann ha scritto:
> Section 8.5/12 says: "The initialization that occurs in [...] and
> brace-enclosed initializer lists is called copy-initialization, and is
> equivalent to the form  T x = a;", which seems to require an accessible copy
> constructor in the expression
>
> T arr[N] = {};

If you read carefully 8.5, you will see that the, despite the name,
copy-initialization does not necessarily require a copy constructor.
That is true for both arrays and non-arrays. This is a common
misunderstanding. There was recently a thread about this on
comp.lang.c++.moderate.

> However, section 8.5.1/7 says: "If there are fewer initializers in the list
> than there are members in the aggregate, then each member not explicitly
> initialized shall be default-initialized (8.5)."

You are referring to an old version of the standard. The 2003 version
reads differently: "If there are fewer initializers in the list than
there are members in the aggregate, then each member not explicitly
initialized shall be value-initialized (8.5)." Notice the
"value-initialized" instead of the "default-initialized". Naively
speaking, value-initialization is either default-initialization or
zero-initialization according to the type of the object and/or the
presence of user defined constructors.

BTW: Always refer to the most recent version of the standard when
submitting defect reports!

> Now the problem is that the standard requires copy-initialization, while not
> providing for an object to be copied. Perhaps the intent is that only
> explicit initializers in the list are copy-initialization, but the standard
> is not clear here.

I agree that the name copy-initialization is confusing and the whole
initialization process is in some cases complex and possibly
unintuitive, but apart from that, the standard is very clear, IMHO.

> The question is whether in
>
> T arr[1] = {};
>
> the object is copy initialized with a temporary that is in turn default
> initialized, such as in
>
> T arr[1] = { T() };
>
> or whether no temporary is created, and the object that is default
> initialized is 'arr[0]' rather than a temporary.

As we saw above, the array member is going to be value-initialized. Such
process, clearly described in the standard, is performed directly on the
target object and does not require the creation/copy of a temporary.

HTH,

Ganesh

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: jth02@arcor.de (Jens Theisen)
Date: Tue, 29 Aug 2006 22:07:40 GMT
Raw View
Alberto Ganesh Barbati wrote:
> If you read carefully 8.5, you will see that the, despite the name,
> copy-initialization does not necessarily require a copy constructor.

I just read it and it seems that

X x = X();

doesn't despite the common belief that it does. Am I reading this correctly?

> You are referring to an old version of the standard. The 2003 version
> reads differently

Interesting. Is this the draft of the new C++0x, or indeed an update of
C++98? I would have thought that this is clearly distinguished and DRs
for C++98 still refer to the old standard.

> I agree that the name copy-initialization is confusing and the whole
> initialization process is in some cases complex and possibly
> unintuitive, but apart from that, the standard is very clear, IMHO.

The old standard was contradicting itself, IMHO, but can only be
interpreted in one sensible way as I see it (that being the latter
correction of Matthias' DR).

> As we saw above, the array member is going to be value-initialized.

My standard doesn't contain that term, I guess it's a C++0x thing?

Jens

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: jdennett@acm.org (James Dennett)
Date: Wed, 30 Aug 2006 16:12:37 GMT
Raw View
Jens Theisen wrote:
> Alberto Ganesh Barbati wrote:

[snip]

>> You are referring to an old version of the standard. The 2003 version
>> reads differently
>
> Interesting. Is this the draft of the new C++0x, or indeed an update of
> C++98? I would have thought that this is clearly distinguished and DRs
> for C++98 still refer to the old standard.

The current C++ standard is ISO/IEC 14882:2003, aka C++03.
C++03 is C++98 combined with TC1, which was not published
separately so far as I know.

(The current draft standard is, as one would expect, from
2006.)

>> I agree that the name copy-initialization is confusing and the whole
>> initialization process is in some cases complex and possibly
>> unintuitive, but apart from that, the standard is very clear, IMHO.
>
> The old standard was contradicting itself, IMHO, but can only be
> interpreted in one sensible way as I see it (that being the latter
> correction of Matthias' DR).
>
>> As we saw above, the array member is going to be value-initialized.
>
> My standard doesn't contain that term, I guess it's a C++0x thing?

A C++03 thing, if memory serves.  C++98 was somewhat defective
in this area; with various corrections folded in it's better,
though naturally still not perfect.

-- James

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Wed, 30 Aug 2006 16:28:25 GMT
Raw View
Greg Herlihy ha scritto:
> "Matthias Hofmann" wrote:
>> The question is whether in
>>
>> T arr[1] =3D {};
>>
>> the object is copy initialized with a temporary that is in turn defaul=
t
>> initialized, such as in
>>
>> T arr[1] =3D { T() };
>>
>> or whether no temporary is created, and the object that is default
>> initialized is 'arr[0]' rather than a temporary.
>=20
> =A78.5/15 clause 6 provides the final, missing step needed to complete
> the above analysis.

=A78.5/15 clause 6 does not apply, because clause 3 "Otherwise, if the
destination type is an array, see 8.5.1." is in effect. The rest of the
reasoning is therefore incorrect as it starts with a wrong assumption.

Ganesh

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Thu, 31 Aug 2006 01:55:41 GMT
Raw View
Matthias Hofmann ha scritto:
> "Alberto Ganesh Barbati" <AlbertoBarbati@libero.it> schrieb im Newsbeitrag
> news:2F%Ig.86371$zy5.1470746@twister1.libero.it...
>> Matthias Hofmann ha scritto:
>
> Given that, the expression
>
> T arr[N] = {};
>
> should require an accessible constructor that takes a T as its argument.
> This can be a copy constructor or a template constructor.

Incorrect. As I already wrote in another post, every element of the
array is simply value-initialized. That is:

1) if T is a built-in type or if T is a class without any user-defined
constructors, then every element is zero-initialized (well... I
oversimplified a bit for classes, just to give you the idea. Actually,
in this case, value-initialization is recursively applied to every
non-static data member and base class, see 8.5/5 for details)
2) if T is a class with an accessible default constructor, then every
element is default-initialized
3) if T is a class with user-defined constructors but without or with an
inaccessible default constructor, then the code is ill-formed.

In *no* cases the copy constructor is either required or used.

> If I understand this correctly, value-initialization does not involve a copy
> constructor. This would make things clear.

Exactly.

>> BTW: Always refer to the most recent version of the standard when
>> submitting defect reports!
>
> Where can I get a copy of the latest draft?

There's a link to it on the C++ Standard Committe website at
http://www.open-std.org/jtc1/sc22/wg21/ (the most recent version
currently is
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf)

HTH,

Ganesh

---
[ 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.comeaucomputing.com/csc/faq.html                      ]