Article View: comp.std.c++
Article #2818Re: Can I declare private constants?
From: Steve Clamage
Date: Thu, 30 Nov 1995 22:54
Date: Thu, 30 Nov 1995 22:54
53 lines
2569 bytes
2569 bytes
In article imb@sunsystem5.informatik.tu-muenchen.de, schuenem@informatik.tu-muenchen.de (Ulf Schuenemann) writes: > >DWP 7.1.5.1 [dcl.type.cv], 2: >> Unless explicitly declared extern, a const object >> does not have external linkage >> and shall be initialized (8.5, 12.1); > ^--(#) > >Does this mean that it _may_ _not_ be initialized >if explicitly declared extern (like (*) sugggests)? No, that would lead to a contradiction. A const object must be intialized, but does not have external linkage unless declared extern. If you could not initialize an object declared extern, you could not have any const objects with external linkage. >The DWP continues: >> ... [can call default-ctor] otherwise if the const object is not >> initialized with an explicit initializer the program is ill-formed. > >So does the "Unless explicitly declared extern" extend beyond >the semicolon (#) up to the definition of ill-formed ? >Then the wording is missleading, at least for my english skills. >Otherwise "extern const int i;" would be ill-formed. Ah, well, it's hard to provide good wording for this stuff. I think the wording is correct as it stands, however. The first sentence says you have to have an initializer if the object is declared const but not extern. The second sentence says that every const object must be initialized; it goes on to say that a default ctor will serve as an initializer. Now suppose you declare extern const int x; The first sentence doesn't require an initializer, since x has external linkage. The second sentence just says x must be initialized. It doesn't say it has to be initialized here. In fact, this line of code is a declaration but is not a definition, so the initialization would appear elsewhere. --- Steve Clamage, stephen.clamage@eng.sun.com --- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<49lcij$i7h@engnews1.eng.sun.com>Path:
arc1-ca.pugleaf.net!128.32.155.1!mattReferences:
<49kge5$imb@sunsystem5.informatik.tu-muenchen.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt From: clamage@Eng.Sun.COM (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Can I declare private constants? Date: 30 Nov 1995 22:54:18 GMT Organization: Sun Microsystems Inc. Approved: matt@physics2.Berkeley.EDU (Matt Austern) Message-ID: <49lcij$i7h@engnews1.eng.sun.com> References: <49kge5$imb@sunsystem5.informatik.tu-muenchen.de> Reply-To: clamage@Eng.Sun.COM NNTP-Posting-Host: physics2.berkeley.edu