Storage class specifiers

From cppreference.com
< cpp‎ | language

The storage class specifiers are a part of the decl-specifier-seq of a declaration syntax. They control two independent properties of the names introduced by the declaration: their storage duration and their linkage.

  • auto - automatic storage duration.
(until C++11)
  • register - automatic storage duration. Also hints to the compiler to place the variable in the processor's register. (deprecated)
  • static - static or thread storage duration and internal linkage
  • extern - static or thread storage duration and external linkage
  • thread_local - thread storage duration.
(since C++11)


Only one storage class specifier may appear in a declaration (except that thread_local may be combined with static or with extern)

Contents

[edit] Explanation

1) The auto specifier was only allowed for variables declared at block scope or in function parameter lists. It indicated automatic storage duration, which is the default for these kinds of declarations. The meaning of this keyword was changed in C++11.
(until C++11)
2) (deprecated) The register specifier is only allowed for variables declared at block scope and in function parameter lists. It indicates automatic storage duration, which is the default for these kinds of declarations. Additionally, the presence of this keyword may be used as a hint for the optimizer to store the value of this variable in a CPU register. This keyword was deprecated in C++11.
3) The static specifier is only allowed in the declarations of variables (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 static member. When used in a declaration of a variable, it specifies static storage duration (except if accompanied by thread_local). When used in a declaration at namespace scope, it specifies internal linkage.
4) The extern 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 variable, so all extern variables have static or thread durations
5) The thread_local keyword is only allowed for variables declared at namespace scope, variables declared at block scope, and static data members. It indicates that the variable has thread storage duration. It can be combined with static or extern to specify internal or external linkage (except for static data members which always have external linkage), respectively, but that additional static doesn't affect the storage duration.
(since C++11)

[edit] Storage duration

All variables in a program have one of the following storage durations:

  • automatic storage duration. The variable is allocated at the beginning of the enclosing code block and deallocated on end. All non-global variables have this storage duration, except those declared static, extern or thread_local.
  • static storage duration. The storage for the variable is allocated when the program begins and deallocated when the program ends. Only one instance of the variable exists. All variables declared at namespace scope (including global namespace) have this storage duration, plus those declared with static or extern.
  • thread storage duration. The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared thread_local have this storage duration. thread_local can appear together with static or extern to adjust linkage.
(since C++11)
  • dynamic storage duration. The variable is allocated and deallocated per request by using dynamic memory allocation functions.

[edit] 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 extern (regardless of the static modifier)
  • Local classes and their member functions
  • Other names declared at block scope such as typedefs, enumerations, and enumerators
  • 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 static
  • variables declared const or constexpr but not extern
  • data members of anonymous unions
In addition, all names declared in unnamed namespace or a namespace within an unnamed namespace, that aren't explicitly declared extern, have internal linkage.
  • external linkage. The name can be referred to from the scopes in the other translation units. Variables and functions with external linkage also have language linkage, 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
  • variables and functions, not listed above (that is, not declared static, and declared const but not extern)
  • enumerations and enumerators
  • names of classes, their member functions, static data members (const or not), nested classes and enumerations, and functions first introduced with friend declarations inside class bodies
  • names of all templates not listed above (that is, not function templates declared static)
Any of the following names first declared at block scope have external linkage
  • names of variables declared extern
  • names of functions

[edit] Static local variables

Static variables declared at block scope are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.

If the initialization throws an exception, 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.

If multiple threads attempt to initialize the same static local variable concurrently, the initialization occurs exactly once (similar behavior can be obtained for arbitrary functions with std::call_once) (since C++11)

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

[edit] Notes

Names at the top-level namespace scope (file scope in C) that are const and not extern have external linkage in C, but internal linkage in C++.

In C, the address of a register variable cannot be taken, but in C++, a variable declared register is semantically indistinguishable from a variable declared without any storage class specifiers.

Names of thread_local variables with internal or external linkage referred from different scopes may refer to the same or to different instances depending on whether the code is executing in the same or in different threads.

The extern keyword can also be used to specify language linkage and explicit template instantiation declarations, but it's not a storage class specifier in those cases (except when a declaration is directly contained in a language linkage specification, in which case the declaration is treated as if it contains the extern specifier).

The keyword mutable is a storage class specifier in the C++ language grammar, although it doesn't affect storage duration or linkage.

[edit] Keywords

auto, register, static, extern, thread_local

[edit] Example

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
 
thread_local unsigned int rage = 1; 
std::mutex cout_mutex;
 
void increase_rage(const std::string& thread_name)
{
    ++rage;
    std::lock_guard<std::mutex> lock(cout_mutex);
    std::cout << "Rage counter for " << thread_name << ": " << rage << '\n';
}
 
int main()
{
    std::thread a(increase_rage, "a"), b(increase_rage, "b");
 
    {
        std::lock_guard<std::mutex> lock(cout_mutex);
        std::cout << "Rage counter for main: " << rage << '\n';
    }
 
    a.join();
    b.join();
 
    return 0;
}

Possible output:

Rage counter for a: 2
Rage counter for main: 1
Rage counter for b: 2