Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The static keyword

Reference for C++ static: function-local statics, internal linkage at namespace scope, static data members, static member functions and initialization order.

static means different things depending on where it appears, but all meanings involve lifetime or linkage. On a local variable it gives static storage duration: the variable is created the first time control passes through its declaration and lives until the program ends. On a namespace-scope name it gives internal linkage: the name is private to the translation unit. On a class member it means the member belongs to the class, not to any object.

C++
int next() { static int n = 0; return ++n; }   // remembers its value between calls
static int helper = 3;                         // private to this .cpp file
struct Id { static int count; };               // one shared counter for all Ids

Try it

C++ Function-local statics and static members

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Internal linkage, static data member definitions, and a lazy singleton

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
static type name = init;            // local, namespace-scope or class member
static return-type func(params);    // static member function, or internal-linkage function
Context Effect
Local variable Static storage duration. Initialized once (thread-safely) the first time its declaration is reached; destroyed at program exit in reverse order of construction.
Namespace-scope variable or function Internal linkage. Prefer an unnamed namespace in modern code.
Static data member One object shared by the class. Needs a single definition unless it is inline or constexpr (C++17).
Static member function Has no this; can access only static members and objects it is given. May be called as Class::f().

Usage notes

  • A static local with a constant initializer needs no guard; with a dynamic initializer, the compiler adds a thread-safe once-flag, so each call pays a small check.
  • Statics are destroyed in reverse order of construction when main returns. Using one from another static's destructor can touch an already-destroyed object.
  • Namespace-scope variables with dynamic initialization in different translation units have no defined order (the static initialization order fiasco). Prefer function-local statics, constinit or constexpr.
  • Do not write static on definitions of class members outside the class. static appears only in the declaration inside the class.
  • A static function in a header gives each including file its own copy. Use inline instead if one shared definition is intended.
  • Static data members are not part of object size: sizeof(Session) does not include alive_.
  • Mutable statics shared between threads need synchronization; the initialization is safe, later modification is not.
  • static inside a lambda or a constexpr function: allowed in C++23 for static operator() and static constexpr variables; before that, static variables are not allowed in constexpr functions.
  • C's meaning of static for array parameters (int a[static 10]) does not exist in C++.