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.
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
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.
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
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
staticlocal 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
mainreturns. 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,
constinitorconstexpr. - Do not write
staticon definitions of class members outside the class.staticappears only in the declaration inside the class. - A
staticfunction in a header gives each including file its own copy. Useinlineinstead if one shared definition is intended. - Static data members are not part of object size:
sizeof(Session)does not includealive_. - Mutable statics shared between threads need synchronization; the initialization is safe, later modification is not.
staticinside a lambda or aconstexprfunction: allowed in C++23 forstaticoperator() andstatic constexprvariables; before that,staticvariables are not allowed inconstexprfunctions.- C's meaning of
staticfor array parameters (int a[static 10]) does not exist in C++.