Storage classes in C++ control three properties of a variable: how long it lives (storage duration), which code can name it (linkage), and where its memory comes from. The storage class specifiers that remain in modern C++ are static, extern, thread_local and mutable. The old auto meaning and register are gone, so auto now only deduces types. This guide explains storage classes C++ programmers actually use and when each one applies.
You already know local variables and functions, so this guide focuses on the rules that decide whether a name is visible in other files and how long its value survives.
Every object has one of four storage durations:
| Duration | Lifetime | Typical example |
|---|---|---|
| Automatic | From the declaration until the enclosing block ends | Ordinary local variables |
| Static | From program start (or first use for function-local statics) until program end | Globals, static variables |
| Thread | From thread start until the thread ends | thread_local variables |
| Dynamic | From new until delete | Objects created with new or smart pointers |
Linkage is a separate idea. It decides whether the same name in different translation units refers to the same entity:
.cpp file after preprocessing).Storage class specifiers are how you adjust these two properties.
A variable declared inside a function without any specifier has automatic storage duration. It is created when execution reaches the declaration and destroyed when the block ends.
Before C++11, auto explicitly meant "automatic storage", which was redundant. C++11 reused the keyword for type deduction, so auto x = 5; now means "deduce int". The register keyword was a hint to keep a variable in a CPU register. It was deprecated in C++11 and removed in C++17, so do not use it; compilers allocate registers better than hand hints.
static has two different meanings depending on where you write it.
A static variable inside a function is initialised once and keeps its value between calls.
Since C++11 the initialisation of a function-local static is thread-safe: if two threads reach it at the same time, one runs the initialiser and the other waits. The variable is destroyed at program exit in reverse order of construction.
At file or namespace scope, static gives a variable or function internal linkage. Two files can each define their own static int count; without clashing.
In modern C++ an unnamed namespace achieves the same thing and also works for types, so many style guides prefer it:
Note that const variables at namespace scope already have internal linkage by default, which is why constants in headers do not cause duplicate-symbol errors.
Inside a class, static means one object shared by every instance, not one per object. Since C++17 you can define it inline in the class:
extern declares a variable that is defined somewhere else, giving it external linkage without creating storage. The declaration goes in a header, and exactly one .cpp file holds the definition.
If you wrote int maxUsers = 100; in the header instead, every file including it would define the variable and the linker would report multiple definitions. For constants, prefer inline constexpr int maxUsers = 100; in the header (C++17), which needs no separate definition.
extern "C" is a different use of the same keyword. It switches a declaration to C language linkage so C++ code can call C libraries without name mangling.
thread_local gives each thread its own copy of a variable. The copy is created when the thread starts (or on first use) and destroyed when the thread exits.
The output lines from the two threads may appear in either order, but each thread counts independently. thread_local can be combined with static or extern, and it is useful for per-thread caches, error codes and random number generators that avoid locking. Remember that each thread pays for its own copy.
mutable applies to a non-static class data member. It allows that member to change even when the object is const, which suits caches and mutex members.
Use it only for state that is not part of the logical value of the object.
Not any more. Since C++11 auto deduces a variable's type, and the old "automatic storage" meaning was removed.
static at namespace scope makes a name private to its file, while extern declares a name that is defined in another file and shared across files.
A static local variable lives for the whole program but is still visible only inside its function. A static at namespace scope is global in lifetime but private to its file.
Their initialisation is thread-safe since C++11. Reading and writing the variable afterwards is not synchronised, so protect shared mutable data yourself.
Storage classes C++ code uses today are static, extern, thread_local and mutable. Think in two axes: storage duration (automatic, static, thread, dynamic) and linkage (none, internal, external). Use static locals for state that persists across calls, unnamed namespaces or static for file-private helpers, extern declarations in headers with a single definition in a source file, and thread_local for per-thread data. Avoid register, which no longer exists, and auto as a storage specifier.