C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Have classes external linkage

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
ToTo
Guest





PostPosted: Mon Dec 19, 2005 10:25 am    Post subject: Have classes external linkage Reply with quote



Have classes external linkage?

I'm in doubt about the truth concerning linkage of classes.

Generally I have two contrary information regarding this:
- Mr. Lakos states in "Large-Scale C++ Software Design", that
classes do have internal linkage, period.
vs.
- As the standard states named classes have external linkage.

When I think about this question logically, classes can not be
declared at all, thus they can not have external linkage.
- A forward declaration does not expose the interface, in conclusion
it is insufficient.
- When the classes body is written (i.e. {} is applied), the resulting
construct is a definition (obeying the ODR) and not a declartion.

I have two theorys interpreting the words I read on this topic:
1. The standard and Mr. Lakos do simply speak about different thinks.
2. (Unlikely) There exist a backdoor comparable to making const being
linked external with the extern keyword.


Can anybody help me learning the truth?

Thank you in advance!

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Dec 19, 2005 1:49 pm    Post subject: Re: Have classes external linkage Reply with quote



In article <43a59b77$0$23875$9b622d9e (AT) news (DOT) freenet.de>, ToTo
<nospam (AT) nospam (DOT) de> writes
Quote:
Have classes external linkage?

I'm in doubt about the truth concerning linkage of classes.

Linkage is a property of names and is determined by the scope in which
they are declared. If Lakos says that classes have internal linkage he
is mistaken. There is, in general, no way to declare a name as a type
without providing it external linkage (though local classes are
different)

I think the confusion may arise because C does not provide external
linkage for the declaration of struct names. It has no need to do so
because it addresses the problem of inter TU compatibility in a
different way, and the name associated with a struct is not in the
general global namespace but in its tag namespace. I think (but have not
looked carefully at the C Standard with regards to this issue) that tag
names are on a per TU basis.

Strictly speaking a class does not have linkage but the name of a class
declared at namespace scope (including global scope) has external
linkage (in effect that means that the wherever the name is used it
should refer to the same type, however name hiding rules need to be
considered as well)


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Greg Herlihy
Guest





PostPosted: Tue Dec 20, 2005 12:30 am    Post subject: Re: Have classes external linkage Reply with quote



Francis Glassborow wrote:
....
Quote:

Strictly speaking a class does not have linkage but the name of a class
declared at namespace scope (including global scope) has external
linkage (in effect that means that the wherever the name is used it
should refer to the same type, however name hiding rules need to be
considered as well)

The notable exception to the "same name /same type" rule of thumb would
be a name declared in an anonymous namespace. A name declared in an
anonymous namespace is guaranteed to refer to a type unique per
translation unit. This named type will not correspond to any other type
of the same name in any other translation unit, even one with an
identical - or even the same (via a shared header file) - declaration.

Anonymous namespaces permit a name to be externally-linked so that it
can be used as a template type parameter; but in all other regards the
name effectively behaves as if it had been declared with file static
scope.

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Tue Dec 27, 2005 4:34 pm    Post subject: Re: Have classes external linkage Reply with quote

Francis Glassborow wrote:
Quote:
In article <43a59b77$0$23875$9b622d9e (AT) news (DOT) freenet.de>, ToTo
[email]nospam (AT) nospam (DOT) de[/email]> writes
Have classes external linkage?

I'm in doubt about the truth concerning linkage of classes.

Linkage is a property of names and is determined by the scope
in which they are declared. If Lakos says that classes have
internal linkage he is mistaken.

I'd first like to see his exact statement. As you say, names
(not types) have linkage. But I would be very surprised if that
was what he was saying. More likely he is referring to the way
most compilers worked, at least when he was writing. Or the
fact that you have to define the class in each translation unit;
the compiler cannot use the name to find the definition
elsewhere. Or the related fact that when you use the name of a
class, that name refers to the definition given in the current
translation unit.

Quote:
There is, in general, no way to declare a name as a type
without providing it external linkage (though local classes
are different)

A name defined by a typedef is the name of a type, but has no
linkage. Along with local classes.

Quote:
I think the confusion may arise because C does not provide
external linkage for the declaration of struct names.

In fact, in C, all user defined type names have no linkage.
Formally, at least: C uses a concept of "compatible types" for
type matching between compilation units, with the rules: "[...],
two structure, union or enumerated types declared in separate
translation units are compatible if their tags and members
satisfy the following requirements: If one is declared with a
tag, the other shall be declared with the same tag. [...]"
Which sort of ends up with similar effects as external linkage,
at least to a certain degree.

Quote:
It has no need to do so because it addresses the problem of
inter TU compatibility in a different way, and the name
associated with a struct is not in the general global
namespace but in its tag namespace. I think (but have not
looked carefully at the C Standard with regards to this issue)
that tag names are on a per TU basis.

Sort of, see above. The tag names have no linkage, but they do
play a role regarding whether two types are compatible.

Quote:
Strictly speaking a class does not have linkage but the name
of a class declared at namespace scope (including global
scope) has external linkage (in effect that means that the
wherever the name is used it should refer to the same type,
however name hiding rules need to be considered as well)

The name has external linkage. The complete rules as to what
that means are fairly complicated; in the case of classes, it
means that the compiler may (and in fact must) consider that all
uses of this name, within the corresponding scope, refer to the
same class. It is up to the programmer, however, to ensure that
the actual class definitions are really the same.

--
James Kanze GABI Software
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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.