cpp/language/storage duration

The storage class specifiers are a part of the of a name's. Together with the of the name, they control two independent properties of the name: its storage duration and its linkage.


 * no specifier - automatic storage duration.


 * - static or thread storage duration and internal linkage (or external linkage for static class members not in an anonymous namespace).
 * - static or thread storage duration and external linkage.


 * - does not affect storage duration or linkage. See for the explanation.

Only one storage class specifier may appear in a declaration.

Explanation
@3@ The specifier is only allowed in the declarations of objects (except in function parameter lists), declarations of functions (except at block scope), and declarations of anonymous unions. When used in a declaration of a class member, it declares a. When used in a declaration of an object, it specifies static storage duration (except if accompanied by ). When used in a declaration at namespace scope, it specifies internal linkage. @4@ The specifier is only allowed in the declarations of variables and functions (except class members or function parameters). It specifies external linkage, and does not technically affect storage duration, but it cannot be used in a definition of an automatic storage duration object, so all objects have static or thread durations. In addition, a variable declaration that uses and has no initializer is not a.

Storage duration
All s in a program have one of the following storage durations:


 * automatic storage duration. The storage for the object is allocated at the beginning of the enclosing code block and deallocated at the end. All local objects have this storage duration, except those declared, or.


 * static storage duration. The storage for the object is allocated when the program begins and deallocated when the program ends. Only one instance of the object exists. All objects declared at namespace scope (including global namespace) have this storage duration, plus those declared with or . See  and Static local variables for details on initialization of objects with this storage duration.


 * dynamic storage duration. The storage for the object is allocated and deallocated upon request by using dynamic memory allocation functions. See for details on initialization of objects with this storage duration.

The storage duration of and reference members is that of their complete object.

Linkage
A name that denotes object, reference, function, type, template, namespace, or value, may have linkage. If a name has linkage, it refers to the same entity as the same name introduced by a declaration in another scope. If a variable, function, or another entity with the same name is declared in several scopes, but does not have sufficient linkage, then several instances of the entity are generated.

The following linkages are recognized:

No linkage
The name can be referred to only from the scope it is in.

Any of the following names declared at block scope have no linkage:
 * variables that aren't explicitly declared (regardless of the  modifier);
 * and their member functions;
 * other names declared at block scope such as typedefs, enumerations, and enumerators.

Names not specified with external or internal linkage also have no linkage, regardless of which scope they are declared in.

Internal linkage
The name can be referred to from all scopes in the current translation unit.

Any of the following names declared at namespace scope have internal linkage:
 * variables, functions, or function templates declared ;
 * variables of non-volatile const-qualified type, unless


 * they are explicitly declared, or
 * they were previously declared and the prior declaration did not have internal linkage;


 * data members of.

External linkage
The name can be referred to from the scopes in the other translation units. Variables and functions with external linkage also have, which makes it possible to link translation units written in different programming languages.

Any of the following names declared at namespace scope have external linkage, unless they are declared in an unnamed namespace:
 * variables and functions not listed above (that is, functions not declared, non-const variables not declared , and any variables declared );
 * enumerations;
 * names of classes, their member functions, static data members (const or not), nested classes and enumerations, and functions first introduced with declarations inside class bodies;
 * names of all templates not listed above (that is, not function templates declared ).

Any of the following names first declared at block scope have external linkage:
 * names of variables declared ;
 * names of functions.

{{rrev|since=c++20|

Module linkage
The name can be referred to only from the scopes in the same module unit or in the other translation units of the same named module.

Names declared at namespace scope have module linkage if their declarations are attached to a named module and are not exported, and don't have internal linkage. }}

Static local variables
Variables declared at block scope with the specifier  have static  storage duration but are initialized the first time control passes through their declaration (unless their initialization is  or, which can be performed before the block is first entered). On all further calls, the declaration is skipped.

If the initialization, the variable is not considered to be initialized, and initialization will be attempted again the next time control passes through the declaration.

If the initialization recursively enters the block in which the variable is being initialized, the behavior is undefined.

The destructor for a block-scope static variable is called at program exit, but only if the initialization took place successfully.

Function-local static objects in all definitions of the same (which may be implicitly inline) all refer to the same object defined in one translation unit, as long as the function has external linkage.

Translation-unit-local entities
The concept of translation-unit-local entities is standardized in C++20, see for more details.

An entity is translation-unit-local (or TU-local for short) if


 * it has a name with internal linkage, or
 * it does not have a name with linkage and is introduced within the definition of a TU-local entity, or
 * it is a template or template specialization whose template argument or template declaration uses a TU-local entity.

Bad things (usually violation of ) can happen if the type of a non-TU-local entity depends on a TU-local entity, or if a declaration of a non-TU-local entity names a TU-local entity outside its


 * function-body for a non-inline function or function template
 * initializer for a variable or variable template
 * friend declarations in a class definition
 * use of value of a variable, if the variable is

Keywords
,, , , ,