Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Storage class specifiers

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
      Declarations
       

      The storage class specifiers are a part of thedecl-specifier-seq of a name'sdeclaration syntax. Together with thescope of the name, they control two independent properties of the name: itsstorage duration and itslinkage.

      Contents

      [edit]Storage duration

      Thestorage duration is the property of anobject that defines the minimum potential lifetime of the storage containing the object. The storage duration is determined by the construct used to create the object and is one of the following:

      • static storage duration
      • thread storage duration (also known as thread-local storage duration)
      (since C++11)
      • automatic storage duration
      • dynamic storage duration

      Static, thread,(since C++11) and automatic storage durations are associated with objects introduced bydeclarations and withtemporary objects. The dynamic storage duration is associated with objects created by anew expression or withimplicitly created objects.

      The storage duration categories apply to references as well.

      The storage duration ofsubobjects and reference members is that of their complete object.

      [edit]Specifiers

      The following keywords arestorage class specifiers :

      • auto
      (until C++11)
      • register
      (until C++17)
      • static
      • thread_local
      (since C++11)
      • extern
      • mutable

      In adecl-specifier-seq, there can be at most one storage class specifier, except thatthread_local may appear withstatic orextern(since C++11).

      mutable has no effect on storage duration. For its usage, seeconst/volatile.

      Other storage class specifiers can appear in thedecl-specifier-seq s of the following declarations:

      SpecifierCan appear in thedecl-specifier-seq s of
      Variable declarationsFunction declarationsStructured binding declarations
      (since C++17)
      Non-memberMemberNon-memberMember
      Non-parameterFunction parameterNon-static Static Non-static Static 
      autoBlock scope onlyYes No No No No NoN/A
      registerBlock scope onlyYes No No No No NoN/A
      staticYes NoDeclares staticNamespace scope onlyDeclares staticYes
       thread_local Yes No NoYes No No NoYes
      externYes No No NoYes No No No

      Anonymous unions can also be declared withstatic.

      register is a hint that the variable so declared will be heavily used, so that its value can be stored in a CPU register. The hint can be ignored, and in most implementations it will be ignored if the address of the variable is taken. This use is deprecated.

      (until C++17)

      [edit]Static storage duration

      A variable satisfying all following conditions hasstatic storage duration :

      • It does not have thread storage duration.
      (since C++11)

      The storage for these entities lasts for the duration of the program.

      Thread storage duration

      All variables declared withthread_local havethread storage duration.

      The storage for these entities lasts for the duration of the thread in which they are created. There is a distinct object or reference per thread, and use of the declared name refers to the entity associated with the current thread.

      (since C++11)

      [edit]Automatic storage duration

      The following variables haveautomatic storage duration :

      • Variables that belong to ablock scope and are not explicitly declaredstatic,thread_local,(since C++11) orextern. The storage for such variables lasts until the block in which they are created exits.
      • Variables that belong to a parameter scope (i.e. function parameters). The storage for a function parameter lasts until immediately after itsdestruction.

      [edit]Dynamic storage duration

      Objects created by the following methods during program execution havedynamic storage duration :

      [edit]Linkage

      A name can haveexternal linkage ,module linkage(since C++20),internal linkage, orno linkage:

      • An entity whose name has module linkage can be redeclared in another translation unit, as long as the redeclaration is attached to the same module.
      (since C++20)
      • An entity whose name has internal linkage can be redeclared in another scope in the same translation unit.
      • An entity whose name has no linkage can only be redeclared in the same scope.

      The following linkages are recognized:

      [edit]No linkage

      Any of the following names declared at block scope have no linkage:

      • variables that are not explicitly declaredextern (regardless of thestatic modifier);
      • local classes and their member functions;
      • other names declared at block scope such as typedefs, enumerations, and enumerators.

      Names not specified with external, module,(since C++20) or internal linkage also have no linkage, regardless of which scope they are declared in.

      [edit]Internal linkage

      Any of the following names declared at namespace scope have internal linkage:

      • variables, variable templates(since C++14), functions, or function templates declaredstatic;
      • non-template(since C++14)variables of non-volatile const-qualified type, unless
      • they are inline,
      (since C++17)
      (since C++20)
      • they are explicitly declaredextern, or
      • they were previously declared and the prior declaration did not have internal linkage;

      In addition, all names declared inunnamed namespaces or a namespace within an unnamed namespace, even ones explicitly declaredextern, have internal linkage.

      (since C++11)

      [edit]External linkage

      Variables and functions with external linkage also havelanguage 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, unless they are declared in an unnamed namespace or their declarations are attached to a named module and are not exported(since C++20):

      • variables and functions not listed above (that is, functions not declaredstatic, non-const variables not declaredstatic, and any variables declaredextern);
      • enumerations;
      • names of classes, their member functions, static data members (const or not), nested classes and enumerations, and functions first introduced withfriend declarations inside class bodies;
      • names of all templates not listed above (that is, not function templates declaredstatic).

      Any of the following names first declared at block scope have external linkage:

      • names of variables declaredextern;
      • names of functions.

      Module linkage

      Names declared at namespace scope have module linkage if their declarations are attached to a named module and are not exported, and do not have internal linkage.

      (since C++20)
      This section is incomplete
      Reason: add the description of the behavior when an entity is declared with different linkages in the same translation unit (6.6 paragraph 6), note the difference between C++20 (ill-formed) and the current draft (well-formed)

      [edit]Static block variables

      Block variables with static or thread(since C++11) storage duration are initialized the first time control passes through their declaration (unless their initialization iszero- orconstant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.

      • If the initializationthrows 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 withstd::call_once).
      • Usual implementations of this feature use variants of the double-checked locking pattern, which reduces runtime overhead for already-initialized local statics to a single non-atomic boolean comparison.
      (since C++11)

      The destructor for a block variable with static storage durationis called at program exit, but only if the initialization took place successfully.

      Variables with static storage duration in all definitions of the sameinline function (which may be implicitly inline) all refer to the same object defined in one translation unit, as long as the function has external linkage.

      [edit]Translation-unit-local entities

      The concept of translation-unit-local entities is standardized in C++20, seethis page for more details.

      An entity istranslation-unit-local (orTU-local for short) if

      • it has a name with internal linkage, or
      • it does not have a name with linkage and is introduced within the definition of a TU-local entity, or
      • it is a template or template specialization whose template argument or template declaration uses a TU-local entity.

      Bad things (usually violation ofODR) can happen if the type of a non-TU-local entity depends on a TU-local entity, or if a declaration of, or adeduction guide for,(since C++17) a non-TU-local entity names a TU-local entity outside its

      • function-body for a non-inline function or function template
      • initializer for a variable or variable template
      • friend declarations in a class definition
      • use of value of a variable, if the variable isusable in constant expressions

      Such uses are disallowed in amodule interface unit (outside its private-module-fragment, if any) or a module partition, and are deprecated in any other context.

      A declaration that appears in one translation unit cannot name a TU-local entity declared in another translation unit that is not a header unit. A declaration instantiated for atemplate appears at the point of instantiation of the specialization.

      (since C++20)

      [edit]Notes

      Names at the top-level namespace scope (file scope in C) that areconst and notextern have external linkage in C, but internal linkage in C++.

      Since C++11,auto is no longer a storage class specifier; it is used to indicate type deduction.

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

      (until C++17)

      In C++, unlike C, variables cannot be declaredregister.

      (since C++17)

      Names ofthread_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.

      Theextern keyword can also be used to specifylanguage linkage andexplicit 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 theextern specifier).

      Storage class specifiers, except forthread_local, are not allowed onexplicit specializations andexplicit instantiations:

      template<class T>struct S{    thread_localstaticint tlm;}; template<>thread_localint S<float>::tlm=0;// "static" does not appear here

      Aconst (may be implied byconstexpr) variable template used to have internal linkage by default, which was inconsistent with other templated entities. Defect reportCWG2387 corrected this.

      (since C++14)
      inline acts as a workaround forCWG2387 by giving external linkage by default. This is why theinline wasadded to many variable templates and thenremoved after having CWG2387 accepted. Standard library implementations also need to useinline as long as a supported compiler has not get CWG2387 implemented. SeeGCC Bugzilla #109126 andMSVC STL PR #4546.(since C++17)
      Feature-test macroValueStdFeature
      __cpp_threadsafe_static_init200806L(C++11)Dynamic initialization and destruction with concurrency

      [edit]Keywords

      auto,register,static,extern,thread_local,mutable

      [edit]Example

      Run this code
      #include <iostream>#include <mutex>#include <string>#include <thread> thread_localunsignedint rage=1;std::mutex cout_mutex; void increase_rage(conststd::string& thread_name){++rage;// modifying outside a lock is okay; this is a thread-local variablestd::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();}

      Possible output:

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

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 216C++98unnamed class and enumeration in class scope have
      different linkage from those in namespace scope
      they all have external
      linkage in these scopes
      CWG 389C++98a name with no linkage should not be
      used to declare an entity with linkage
      a type without linkage shall not be used
      as the type of a variable or function
      with linkage, unless the variable
      or function has C language linkage
      CWG 426C++98an entity could be declared with both internal
      and external linkage in the same translation unit
      the program is ill-formed in this case
      CWG 527C++98the type restriction introduced by the resolution of CWG
      389 was also applied to variables and functions that
      cannot be named outside their own translation units
      the restriction is lifted for these
      variables and functions (i.e. with no
      linkage or internal linkage, or declared
      within unnamed namespaces)
      CWG 809C++98register served very little functiondeprecated
      CWG 1648C++11static was implied even if
      thread_local is combined withextern
      implied only if no other storage
      class specifier is present
      CWG 1686C++98
      C++11
      the name of a non-static variable declared in namespace
      scope had internal linkage only if it is explicitly
      declaredconst (C++98) orconstexpr (C++11)
      only required the type
      to be const-qualified
      CWG 2019C++98the storage duration of reference
      members were unspecified
      same as their complete object
      CWG 2387C++14unclear whether const-qualified variable
      template have internal linkage by default
      const qualifier does not affect
      the linkage of variable
      templates or their instances
      CWG 2533C++98the storage duration of implicitly-
      created objects were unclear
      made clear
      CWG 2850C++98it was unclear when the storage for
      function parameters are deallocated
      made clear
      CWG 2872C++98the meaning of “can be referred to” was unclearimproved wording
      P2788R0C++20declaring a const-qualified variable in a namespace
      gave it internal linkage even in a module unit
      internal linkage is not given

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 6.7.5 Storage duration [basic.stc]
      • C++20 standard (ISO/IEC 14882:2020):
      • 6.7.5 Storage duration [basic.stc]
      • C++17 standard (ISO/IEC 14882:2017):
      • 6.7 Storage duration [basic.stc]
      • C++14 standard (ISO/IEC 14882:2014):
      • 3.7 Storage duration [basic.stc]
      • C++11 standard (ISO/IEC 14882:2011):
      • 3.7 Storage duration [basic.stc]
      • C++03 standard (ISO/IEC 14882:2003):
      • 3.7 Storage duration [basic.stc]
      • C++98 standard (ISO/IEC 14882:1998):
      • 3.7 Storage duration [basic.stc]

      [edit]See also

      C documentation forstorage duration
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/storage_duration&oldid=183348"

      [8]ページ先頭

      ©2009-2025 Movatter.jp