Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Variable template(since C++14)

      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
       
      Expressions
      General
      Literals
      Operators
      Conversions
       
       

      A variable template defines a family of variables or static data members.

      Contents

      [edit]Syntax

      template<parameter-list>variable-declaration (1)
      template<parameter-list>requiresconstraintvariable-declaration (2)(since C++20)
      variable-declaration - adeclaration of a variable. The declared variable name becomes a template name.
      parameter-list - a non-empty comma-separated list of thetemplate parameters, each of which is eitherconstant parameter, atype parameter, atemplate parameter, or aparameter pack of any of those.
      constraint - aconstraint expression which restricts the template parameters accepted by this variable template

      [edit]Explanation

      A variable instantiated from a variable template is called aninstantiated variable. A static data member instantiated from a static data member template is called aninstantiated static data member.

      A variable template may be introduced by a template declaration at namespace scope, wherevariable-declaration declares a variable.

      template<class T>constexpr T pi= T(3.1415926535897932385L);// variable template template<class T>T circular_area(T r)// function template{return pi<T>* r* r;// pi<T> is a variable template instantiation}

      When used at class scope, variable template declares a static data member template.

      usingnamespace std::literals;struct matrix_constants{template<class T>using pauli= hermitian_matrix<T,2>;// alias template template<class T>// static data member templatestaticconstexpr pauli<T> sigmaX={{0,1},{1,0}}; template<class T>staticconstexpr pauli<T> sigmaY={{0,-1i},{1i,0}}; template<class T>staticconstexpr pauli<T> sigmaZ={{1,0},{0,-1}};};

      As with otherstatic members, a definition of a static data member template may be required. Such definition is provided outside the class definition. A template declaration of a static data member at namespace scope may also be a definition of a non-templatedata member of a class template:

      struct limits{template<typename T>staticconst T min;// declaration of a static data member template}; template<typename T>const T limits::min={};// definition of a static data member template template<class T>class X{static T s;// declaration of a non-template static data member of a class template}; template<class T>T X<T>::s=0;// definition of a non-template data member of a class template

      Unless a variable template wasexplicitly specialized or explicitly instantiated, it is implicitly instantiated when a specialization of the variable template is referenced in a context that requiresa variable definition to exist or if the existence of the definition affects the semantics of the program, i.e. if the variable isneeded for constant evaluation by an expression (the definition may be not used).

      The existence of a definition of a variable is considered to affect the semantics of the program if the variable is needed for constant evaluation by an expression, even if constant evaluation of the expression is not required or if constant expression evaluation does not use the definition.

      [edit]Notes

      Until variable templates were introduced in C++14, parametrized variables were typically implemented as either static data members of class templates or as constexpr function templates returning the desired values.

      Variable templates cannot be used astemplate template arguments.

      Feature-test macroValueStdFeature
      __cpp_variable_templates201304L(C++14)Variable templates

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 2255C++14it was unclear whether a specialization of a static
      data member template is a static data member
      it is
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/variable_template&oldid=182727"

      [8]ページ先頭

      ©2009-2025 Movatter.jp