Topic: file scope const objects default linkage
Author: Steve Clamage <clamage@eng.sun.com>
Date: Thu, 13 Dec 2001 00:14:04 GMT Raw View
On Wed, 12 Dec 2001, Steve Clamage wrote:
> On Wed, 12 Dec 2001, Radoslav Getov wrote:
> >
> > given the declaration (at file scope):
> >
> > const MyType x;
> >
> > Is the linkage for 'x' internal or external?
>
> Assuming MyType is a class with a default constructor, x has internal
> linkage. Otherwise, the code is invalid because x requires an initializer.
>
> Taking an easier example first:
>
> const int j = 1; // definition with internal linkage
> extern const int k = 2; // definition with external linkage
> extern const int k; // declaration (not definition) with external linkage
>
> Now let's get back to MyType with a default constructor.
>
> const MyType x; // definition with internal linkage
> extern const MyType x; // declaration (not definition) with external linkage
>
> To get a definition with external linkage, I think you must write
> extern const MyType x( MyType() );
Thanks to Thomas Maeder for pointing out an error. That declaration
really declares x to be a function taking a parameter of type
"pointer to function returning a MyType by value". It is equivalent to
extern const MyType x( MyType(*)() );
To define an object x you can write
extern const MyType x( (MyType()) );
The extra parens around MyType() prevent the interpretation as a type,
since extra parens around types in most contexts are not allowed.
Of course, you can also write
extern const MyType x = MyType();
which avoids the ambiguity entirely.
--
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.research.att.com/~austern/csc/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: Thu, 13 Dec 2001 13:44:38 CST Raw View
In article <165529e2.0112121122.4e67156f@posting.google.com>,
Radoslav Getov <rgetov@hotmail.com> wrote:
>given the declaration (at file scope):
>
>const MyType x;
>
>Is the linkage for 'x' internal or external?
Depending upon what MyType is (say a class vs a builtin type)
the above may or may not be an error (because it may or may not
be considered to have an initializer). But anyway, as to your
specific question, it has internal linkage (in C, it would have
external linkage). To give it external linkage, you need to
extern it (assuming there is no static definition before it).
--
Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
---
[ 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 ]
Author: rgetov@hotmail.com (Radoslav Getov)
Date: Fri, 14 Dec 2001 13:24:32 CST Raw View
comeau@panix.com (Greg Comeau) wrote in message news:<9vamve$292$1@panix3.panix.com>...
> In article <165529e2.0112121122.4e67156f@posting.google.com>,
> Radoslav Getov <rgetov@hotmail.com> wrote:
> >given the declaration (at file scope):
> >
> >const MyType x;
> >
> >Is the linkage for 'x' internal or external?
>
[snip]
> But anyway, as to your
> specific question, it has internal linkage (in C, it would have
> external linkage). To give it external linkage, you need to
> extern it (assuming there is no static definition before it).
Are you sure about C? I thought it would be the same (i.e. const
objects at file scope being 'static' by default). Or is there a difference
for build in types and user types there?
[sinp]
Thanks,
Radoslav Getov
---
[ 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 ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Sat, 15 Dec 2001 08:51:20 CST Raw View
Radoslav Getov wrote:
...
> Are you sure about C? I thought it would be the same (i.e. const
> objects at file scope being 'static' by default). Or is there a difference
> for build in types and user types there?
C and C++ have different rules on this. In C, there's no linkage
difference difference between const and and non-const objects. In C,
named constants are created by using #define. C++ changed some of the
rules, to allow const objects to default to static, which allows
compilers to not actually set aside set aside seperate storage for them,
unless their address is taken in that module. This allows constants to
by type-safe and namespace sensitive.
---
[ 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 ]
Author: comeau@panix.com (Greg Comeau)
Date: Mon, 17 Dec 2001 15:51:56 GMT Raw View
In article <165529e2.0112140508.7ab678f8@posting.google.com>,
Radoslav Getov <rgetov@hotmail.com> wrote:
>comeau@panix.com (Greg Comeau) wrote in message news:<9vamve$292$1@panix3.panix.com>...
>> In article <165529e2.0112121122.4e67156f@posting.google.com>,
>> Radoslav Getov <rgetov@hotmail.com> wrote:
>> >given the declaration (at file scope):
>> >
>> >const MyType x;
>> >
>> ...it has internal linkage (in C, it would have
>> external linkage). To give it external linkage, you need to
>> extern it (assuming there is no static definition before it).
>
>Are you sure about C?
I'm sure. So "co-ed" header files need to consider this.
>I thought it would be the same (i.e. const
>objects at file scope being 'static' by default).
It's not.
>Or is there a difference
>for build in types and user types there?
The things is that C structs are not as user-defineable as C++'s,
and as well, in C++ we have more constant'y consts.
--
Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
---
[ 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 ]
Author: rgetov@hotmail.com (Radoslav Getov)
Date: Wed, 12 Dec 2001 19:25:14 GMT Raw View
Hi,
given the declaration (at file scope):
const MyType x;
Is the linkage for 'x' internal or external?
Thanks,
Radoslav Getov
---
[ 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 ]
Author: Steve Clamage <clamage@eng.sun.com>
Date: Wed, 12 Dec 2001 21:42:59 GMT Raw View
On Wed, 12 Dec 2001, Radoslav Getov wrote:
>
> given the declaration (at file scope):
>
> const MyType x;
>
> Is the linkage for 'x' internal or external?
Assuming MyType is a class with a default constructor, x has internal
linkage. Otherwise, the code is invalid because x requires an initializer.
Taking an easier example first:
const int j = 1; // definition with internal linkage
extern const int k = 2; // definition with external linkage
extern const int k; // declaration (not definition) with external linkage
Now let's get back to MyType with a default constructor.
const MyType x; // definition with internal linkage
extern const MyType x; // declaration (not definition) with external linkage
To get a definition with external linkage, I think you must write
extern const MyType x( MyType() );
--
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.research.att.com/~austern/csc/faq.html ]