 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Saul Guest
|
Posted: Thu Sep 11, 2003 3:34 pm Post subject: typedef linkage |
|
|
Hi,
I've stumbled across the following line in C++PL (special edition):
-----
9.2 (last line in page 199) -
By default, consts and typedefs have internal linkage.
-----
What does linkage has to do with typedefs? Can a typedef be external?
Thanks,
Saul
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Fri Sep 12, 2003 10:27 am Post subject: Re: typedef linkage |
|
|
"Saul" <stamari (AT) bigfoot (DOT) com> wrote in message
| Quote: | I've stumbled across the following line in C++PL (special edition):
-----
9.2 (last line in page 199) -
By default, consts and typedefs have internal linkage.
-----
What does linkage has to do with typedefs? Can a typedef be external?
|
I think it means this ==>
// a.h
// b.h
// a.cpp
#include "a.h"
typedef int Junk;
namespace { void f() { Junk j; } }
// b.cpp
#include "b.h"
typedef double Junk;
namespace { void f() { Junk j; } }
// main.cpp
#include "a.h"
#include "b.h"
int main() { }
The above program should link. If typedefs had external linkage, then the
program would fail to link because a.obj and b.obj both define a symbol
Junk.
--
+++++++++++
Siemel Naran
[ 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: Fri Sep 12, 2003 10:30 am Post subject: Re: typedef linkage |
|
|
[email]stamari (AT) bigfoot (DOT) com[/email] (Saul) wrote in message
news:<62ce112c.0309110053.16ac3562 (AT) posting (DOT) google.com>...
| Quote: | I've stumbled across the following line in C++PL (special edition):
-----
9.2 (last line in page 199) -
By default, consts and typedefs have internal linkage.
-----
What does linkage has to do with typedefs?
|
All names have linkage. A typedef defines a name. The name it defines
must have some sort of linkage.
| Quote: | Can a typedef be external?
|
No. §7.1.1/5: "The extern specifier can be applied only to names of
objects and functions and to anonymous unions." The name defined by a
typedef doesn't qualify.
On the other hand, trying to apply the text in §3.5 to typedef's leads
to some contradictions -- but I think it is a problem in the wording of
§3.5 (or my understanding of it). Basically, §3.5/2 defines the three
types of linkage with regards to the "entity it [the name] denotes". In
the case of a typedef, this would be the actual type. I don't think
that this is what was meant -- it shouldn't be a question of being able
to refer to the entity "by names from other scopes", but to refer to it
"by the same name from other scopes". (But even that is ambiguous -- if
I write "typedef int x", in two different scopes, I can refer to the
type int using the name x in both scopes.)
At any rate, I'm fairly sure that the intention is for names defined by
a typedef to have internal linkage if the typedef is at namespace scope,
and no linkage if the typedef is at block scope. Although I'm not quite
sure how to find this in the standard.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Saul Guest
|
Posted: Sat Sep 13, 2003 8:17 am Post subject: Re: typedef linkage |
|
|
"Siemel Naran" wrote in message >
[snip]
| Quote: | The above program should link. If typedefs had external linkage, then the
program would fail to link because a.obj and b.obj both define a symbol
Junk.
|
This program should indeed link but not because of the internal
typedefs linkage but because typedefs are type aliases and are not
true types. In the example you gave no type 'Junk' is actually created
by the compiler, The type 'int' is used wherever 'Junk' is mentioned.
Saul
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Wed Sep 17, 2003 7:58 pm Post subject: Re: typedef linkage |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] writes:
| Quote: | stamari (AT) bigfoot (DOT) com (Saul) wrote in message
news:<62ce112c.0309110053.16ac3562 (AT) posting (DOT) google.com>...
I've stumbled across the following line in C++PL (special edition):
-----
9.2 (last line in page 199) -
By default, consts and typedefs have internal linkage.
-----
What does linkage has to do with typedefs?
All names have linkage.
|
That statement is confusing. Some names no linkage. See 3.5/2, in
particular
-- when a name has /no linkage/, the entity it denotes cannot be
referred to by names from other scopes.
You may consider "no linkage" as a kind of linkage, but then you'd
have to reconcile that reading with
A name is said to have /linkage/ when it might denote the same object,
reference, function, type, template, namespace or value as a name
introduced by a declaration in another scope:
which strongly suggest that a name cannot have linkage, and this
possibility is in better adequacy with the first quote. You also have
to take into account
The name of a function declared in block scope, and the name of an
object declared by a block scope extern declaration, have linkage.
which also strongly suggests that the names of some entities do not
have linkage.
[...]
| Quote: | I don't think
that this is what was meant -- it shouldn't be a question of being able
to refer to the entity "by names from other scopes", but to refer to it
"by the same name from other scopes".
|
I can come to agree with that.
| Quote: | At any rate, I'm fairly sure that the intention is for names defined by
a typedef to have internal linkage if the typedef is at namespace scope,
|
that would have to be weighted against
namespace N {
typedef struct {
int bar;
} foo;
}
where the typedef-name N::foo has an external linkage.
| Quote: | and no linkage if the typedef is at block scope. Although I'm not quite
sure how to find this in the standard.
|
A typedef-name being syntactically equivalent to a keyword, I would
think that a typedef itself do not have a linkage, except when it
designates an unnamed class. At least that would be consistent with
3.5/8
[...] If a declaration uses a typedef name, it is the linkage of the
type name to which the typedef refers that is considered.
--
Gabriel Dos Reis, [email]dosreis (AT) cmla (DOT) ens-cachan.fr[/email]
[ 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: Fri Sep 19, 2003 10:29 am Post subject: Re: typedef linkage |
|
|
Gabriel Dos Reis <dosreis (AT) cmla (DOT) ens-cachan.fr> wrote
| Quote: | kanze (AT) gabi-soft (DOT) fr writes:
| [email]stamari (AT) bigfoot (DOT) com[/email] (Saul) wrote in message
| news:<62ce112c.0309110053.16ac3562 (AT) posting (DOT) google.com>...
| > I've stumbled across the following line in C++PL (special edition):
| > -----
| > 9.2 (last line in page 199) -
| > By default, consts and typedefs have internal linkage.
| > -----
| > What does linkage has to do with typedefs?
| All names have linkage.
That statement is confusing.
|
Isn't it, though. Perhaps "all names have a type of linkage"?
Anyhow, the point I was really trying to make was that linkage is
associated with names; in the sense that a name has in some way a
linkage attribute (which in data base terms, might be null).
| Quote: | [...]
| At any rate, I'm fairly sure that the intention is for names defined by
| a typedef to have internal linkage if the typedef is at namespace scope,
that would have to be weighted against
namespace N {
typedef struct {
int bar;
} foo;
}
where the typedef-name N::foo has an external linkage.
|
Good point. I'd forgotten about this C compatibility hack (which
basically results in the typedef'ed name being treated as if it has
followed the keyword struct, without the typedef).
| Quote: | | and no linkage if the typedef is at block scope. Although I'm not
| quite sure how to find this in the standard.
A typedef-name being syntactically equivalent to a keyword, I would
think that a typedef itself do not have a linkage, except when it
designates an unnamed class. At least that would be consistent with
3.5/8
[...] If a declaration uses a typedef name, it is the linkage of the
type name to which the typedef refers that is considered.
|
Good point. And when the typedef name is used elsewhere? I think it is
always "as if" the actual type had been used. If so, then it would make
sense to say that the typedef'ed name has no linkage.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Fri Sep 19, 2003 11:19 am Post subject: Re: typedef linkage |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] writes:
[...]
| Quote: | | and no linkage if the typedef is at block scope. Although I'm not
| quite sure how to find this in the standard.
A typedef-name being syntactically equivalent to a keyword, I would
think that a typedef itself do not have a linkage, except when it
designates an unnamed class. At least that would be consistent with
3.5/8
[...] If a declaration uses a typedef name, it is the linkage of the
type name to which the typedef refers that is considered.
Good point. And when the typedef name is used elsewhere? I think it is
always "as if" the actual type had been used.
|
yes, that essentially is what I understand by "a typedef-name is
syntactically equivalent to a keyword and names the type associated
with the identifier"
It is kind of "scoped macro" -- yes, the analogy is poor :-/
| Quote: | If so, then it would make
sense to say that the typedef'ed name has no linkage.
|
that would be my inclination.
--
Gabriel Dos Reis <gdr (AT) integrable-solutions (DOT) net>
[ 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
|
|