Topic: defect report [basic.link]/3 refers to references explicitly declared const
Author: "Alf P. Steinbach" <alfps@start.no>
Date: Mon, 4 May 2009 18:57:07 CST Raw View
* James Kanze:
> [basic.link]/3:
>
> A name having namespace scope (3.3.5) has internal
> linkage if it is the name of
>
> -- [...]
>
> -- an object or reference that is explicitly declared
> const and neither explicitly declared extern nor
> previously declared to have external linkage;[...]
>
> What is meant by the use of reference here? My understanding
> was that a reference can never be explicitly declared const; if
> this is the case, and in fact, a reference at namespace scope
> can only have internal linkage if it is declared static, then
> the phrase "or reference" should be deleted in the above. If
> what is meant is that something like:
> int const& ri = 42 ;
> has internal linkage, then the wording should be changed to say
> that all references to const objects declared at namespace scope
> and not otherwise declared extern have internal linkage. And if
> the meaning is that something like:
>
> typedef int const& RI ;
> RI const ri = 42 ;
>
> should have internal linkage, then the wording in [del.ref]
> which says that "[...]when the cv-qualifiers are introduced
> through the use of a typedef (7.1.3) or of a template type
> argument (14.3), in which case the cv-qualifiers are ignored."
> should be changed, since in this case, the const isn't being
> ignored.
>
> My own preference here would be to go along with what most
> compilers do. (In other words, there's no real reason not to
> conform with existing practice.) The only compiler I have here
> to check with is g++, which seems to conform to the first
> suggestion (dropping the "or reference" from the clause); if
> other compilers behave likewise, that's what I'd suggest.
Two example compilers that conform to the first suggestion, that the
reference
has external linkage:
C:\temp> type x.cpp
#include <iostream>
extern int const& x;
int main()
{
std::cout << x << std::endl;
}
C:\temp> type y.cpp
int const& x = 42;
C:\temp> (g++ --version 2>&1) | find /i "g++"
g++ (GCC) 3.4.5 (mingw-vista special r3)
C:\temp> g++ -pedantic -std=c++98 x.cpp y.cpp -o a & a
42
C:\temp> (cl 2>&1) | find /i "version"
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.6030 for 80x86
C:\temp> cl /nologo /GX /GR x.cpp y.cpp -o b & b
x.cpp
y.cpp
Generating Code...
42
C:\temp> _
Cheers,
- Alf
--
Due to hosting requirements I need visits to <url:
http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Greg Herlihy <greghe@mac.com>
Date: Mon, 4 May 2009 20:19:32 CST Raw View
On May 3, 1:39 pm, James Kanze <james.ka...@gmail.com> wrote:
> [basic.link]/3:
>
> A name having namespace scope (3.3.5) has internal
> linkage if it is the name of
> -- an object or reference that is explicitly declared
> const and neither explicitly declared extern nor
> previously declared to have external linkage;[...]
>
> What is meant by the use of reference here? My understanding
> was that a reference can never be explicitly declared const;
A const reference is declared with the "const" keyword in the same way
as any other const variable in C++. So, in this case,
int const& ri = 42
declares a const reference named "ri".
> If what is meant is that something like:
> int const& ri = 42 ;
> has internal linkage, then the wording should be changed to say
> that all references to const objects declared at namespace scope
> and not otherwise declared extern have internal linkage.
But a "const reference" and a "reference to a const object" have two,
distinctly different meanings (after all, a const reference does not
necesssarily refer to a const object). Therefore, adopting this new
wording would change the current linkage of certain types of
references. For example:
int i = 5;
const int& ri = i;
According to the current wording, the variable "ri" (being a const
reference) has internal linkage. Under the new wording. however, "ri"
- (because it is not a reference to a const object) - would have
external linkage.
So I think that the Standard's existing wording with regard to the
linkage of const references is fine as is.
Greg
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: daniel.kruegler@googlemail.com
Date: Mon, 4 May 2009 22:41:14 CST Raw View
On 3 Mai, 22:39, James Kanze <james.ka...@gmail.com> wrote:
> [basic.link]/3:
>
> A name having namespace scope (3.3.5) has internal
> linkage if it is the name of
>
> -- [...]
>
> -- an object or reference that is explicitly declared
> const and neither explicitly declared extern nor
> previously declared to have external linkage;[...]
>
> What is meant by the use of reference here? My understanding
> was that a reference can never be explicitly declared const; if
> this is the case, and in fact, a reference at namespace scope
> can only have internal linkage if it is declared static, then
> the phrase "or reference" should be deleted in the above.
Exactly this has recently be done. The current WP N2857
has this realized, I think it was due to
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#571
Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: James Kanze <james.kanze@gmail.com>
Date: Sun, 3 May 2009 14:39:00 CST Raw View
[basic.link]/3:
A name having namespace scope (3.3.5) has internal
linkage if it is the name of
-- [...]
-- an object or reference that is explicitly declared
const and neither explicitly declared extern nor
previously declared to have external linkage;[...]
What is meant by the use of reference here? My understanding
was that a reference can never be explicitly declared const; if
this is the case, and in fact, a reference at namespace scope
can only have internal linkage if it is declared static, then
the phrase "or reference" should be deleted in the above. If
what is meant is that something like:
int const& ri = 42 ;
has internal linkage, then the wording should be changed to say
that all references to const objects declared at namespace scope
and not otherwise declared extern have internal linkage. And if
the meaning is that something like:
typedef int const& RI ;
RI const ri = 42 ;
should have internal linkage, then the wording in [del.ref]
which says that "[...]when the cv-qualifiers are introduced
through the use of a typedef (7.1.3) or of a template type
argument (14.3), in which case the cv-qualifiers are ignored."
should be changed, since in this case, the const isn't being
ignored.
My own preference here would be to go along with what most
compilers do. (In other words, there's no real reason not to
conform with existing practice.) The only compiler I have here
to check with is g++, which seems to conform to the first
suggestion (dropping the "or reference" from the clause); if
other compilers behave likewise, that's what I'd suggest.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]