Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What are storage classes in C++? extern, static, thread_local

C++
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 27, 2026
8 min read

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.

Table of Contents

  • Storage duration and linkage in C++
  • Automatic variables and the removed register keyword
  • Using static for local variables and internal linkage
    • Static local variables
    • Static at namespace scope
    • Static data members
  • Sharing globals with extern
  • thread_local for per-thread state
  • The mutable specifier
  • Frequently Asked Questions
    • Is auto a storage class in C++?
    • What is the difference between static and extern?
    • Does static make a variable global?
    • Are function-local statics thread-safe?
  • Summary
  • Related Articles

Storage duration and linkage in C++

Every object has one of four storage durations:

DurationLifetimeTypical example
AutomaticFrom the declaration until the enclosing block endsOrdinary local variables
StaticFrom program start (or first use for function-local statics) until program endGlobals, static variables
ThreadFrom thread start until the thread endsthread_local variables
DynamicFrom new until deleteObjects 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:

  • No linkage: local variables are visible only in their block.
  • Internal linkage: the name is private to one translation unit (one .cpp file after preprocessing).
  • External linkage: the name refers to the same entity across all translation units.

Storage class specifiers are how you adjust these two properties.

Automatic variables and the removed register keyword

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.

cpp
text

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.

Using static for local variables and internal linkage

static has two different meanings depending on where you write it.

Static local variables

A static variable inside a function is initialised once and keeps its value between calls.

cpp
text

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.

Static at namespace scope

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.

cpp

In modern C++ an unnamed namespace achieves the same thing and also works for types, so many style guides prefer it:

cpp

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.

Static data members

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:

cpp

Sharing globals with extern

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.

cpp
text

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.

cpp

thread_local for per-thread state

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.

cpp
text

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.

The mutable specifier

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.

cpp

Use it only for state that is not part of the logical value of the object.

Frequently Asked Questions

Is auto a storage class in C++?

Not any more. Since C++11 auto deduces a variable's type, and the old "automatic storage" meaning was removed.

What is the difference between static and extern?

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.

Does static make a variable global?

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.

Are function-local statics thread-safe?

Their initialisation is thread-safe since C++11. Reading and writing the variable afterwards is not synchronised, so protect shared mutable data yourself.

Summary

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.

Related Articles

  • What is a static variable in C++?
  • What are namespaces in C++ and how do you use them?
  • Learn C++: Complete Study Roadmap from Beginner to Advanced
Share