Scope
This article primarily discusses about the linkage types with a C++ perspective, at the same time giving an insight as to how the things differ from the C language.
Introduction
Identifier names are used in C++ to refer to various types, their instances and constructs like namespaces, templates, references etc. Linkage determines the portions of the program/translation units in which an identifier can be referenced, in other words its visibility. Linkage are of three kinds: internal linkage, external linkage and no linkage.
Linkage Types
Internal Linkage : A name having internal linkage type denotes an identifier which can be referenced using names declared in the same scope or in other scopes of the same translation unit. In other words internally linked identifiers are unique to a tranlation unit. The following kinds of identifiers have internal linkage.
Note 1: Some listings put members of unnamed namespace in internal linkage category but it's not implied. To quote comment [82] in Article 7.3.1.1 in the C++ standard - "Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit"
Note 2: Inline functions in C are of file scope whereas they have external linkage by default in C++.
External Linkage : A name with external linkage can be referred to from every translation unit of the program. The following kinds of identifiers have external linkage.
No Linkage : Names with no linkage are visible only in the scope in which they're declared. Such names include local objects, local classes, and other local types. Put differently, any name that has neither external linkage nor internal linkage has no linkage. Some exceptions to local declarations are entities declared with the extern keyword.
Linkage for functions and objects
The tabular representations below capture various scenarios resulting in varied linkage types for the functions and objects.
* The name has internal linkage if previously declared with internal linkage in a visible declarationStorage class specifier File scope (C) or namespace scope (C++) Block scope Class scope (C++ only) auto invalid invalid invalid extern external linkage, possibly internal linkage* external linkage, possibly internal linkage* invalid register invalid invalid invalid static internal linkage invalid external linkage none external linkage, possibly internal linkage* external linkage, possibly internal linkage* external linkage
Reversing the order of redeclaration with a different storage specifier leads to undefined results. To be more specific an identifier declared as extern before declaring it as static. In such a scenario the compiler does not complain, but results are also not defined.
* The name has internal linkage if previously declared with internal linkage in a visible declarationStorage class specifier File scope (C) or namespace scope (C++) Block scope Class scope (C++ only) auto invalid no linkage; automatic storage invalid extern external linkage, possibly internal linkage*; static storage external linkage, possibly internal linkage*; static storage invalid register invalid no linkage; automatic storage invalid static internal linkage; static storage no linkage; static storage external linkage; static storage none external linkage, except that const objects in C++ have internal linkage; static storage no linkage; automatic storage no linkage; same storage duration as the enclosing object
References
[1] Linkage in C and C++
[2] informIT - C++ Reference Guide (Linkage Types)
[3] C++ Standard
Tuesday, March 25, 2008
Linkage Types - C++
Labels:
C++,
Linkage Types in C++
Subscribe to:
Post Comments (Atom)

What people said... (0)
Post a Comment