Tuesday, March 25, 2008

Linkage Types - C++

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.

  • Objects, references, or functions explicitly declared static.

  • Objects or references declared in namespace scope (or global scope in C) with the specifier const and neither explicitly declared extern, nor previously declared to have external linkage.

  • Data members of an anonymous union. (Refer C++ Standard - Article 9.5.3)

  • a typedef name

  • C++ template function declared as static


  • 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.

  • Objects, references, or functions at global or namespace scope

  • A named class or enumeration

  • const objects explicitly declared extern

  • enumerations and their enumerators

  • templates

  • namespaces


  • 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.



































    Linkage for functions
    Storage class specifierFile scope (C) or namespace scope (C++)Block scopeClass scope (C++ only)
    autoinvalidinvalidinvalid
    externexternal linkage, possibly internal linkage*external linkage, possibly internal linkage*invalid
    registerinvalidinvalidinvalid
    staticinternal linkageinvalidexternal linkage
    noneexternal linkage, possibly internal linkage*external linkage, possibly internal linkage*external linkage
    * The name has internal linkage if previously declared with internal linkage in a visible declaration

    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.



































    Linkage and storage for objects
    Storage class specifierFile scope (C) or namespace scope (C++)Block scopeClass scope (C++ only)
    autoinvalidno linkage; automatic storageinvalid
    externexternal linkage, possibly internal linkage*; static storageexternal linkage, possibly internal linkage*; static storageinvalid
    registerinvalidno linkage; automatic storageinvalid
    staticinternal linkage; static storageno linkage; static storageexternal linkage; static storage
    noneexternal linkage, except that const objects in C++ have internal linkage; static storageno linkage; automatic storageno linkage; same storage duration as the enclosing object
    * The name has internal linkage if previously declared with internal linkage in a visible declaration

    References

    [1] Linkage in C and C++
    [2] informIT - C++ Reference Guide (Linkage Types)
    [3] C++ Standard

    What people said... (0)