Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::tuple_size

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
      Defined in header<array>
      Defined in header<tuple>
      Defined in header<utility>
      Defined in header<ranges>
      (since C++20)
      Defined in header<complex>
      (since C++26)
      template<class T>
      struct tuple_size;// not defined
      (1)(since C++11)
      template<class T>

      struct tuple_size<const T>

         :std::integral_constant<std::size_t, std::tuple_size<T>::value>{};
      (2)(since C++11)
      template<class T>

      struct tuple_size<volatile T>

         :std::integral_constant<std::size_t, std::tuple_size<T>::value>{};
      (3)(since C++11)
      (deprecated in C++20)
      template<class T>

      struct tuple_size<constvolatile T>

         :std::integral_constant<std::size_t, std::tuple_size<T>::value>{};
      (4)(since C++11)
      (deprecated in C++20)

      Provides access to the number of elements in atuple-like type as a compile-time constant expression.

      1) The primary template is not defined. An explicit (full) or partial specialization is required to make a type tuple-like.
      2-4) Specializations for a cv-qualified types reuse thevalue from the corresponding cv-unqualified versions by default.

      std::tuple_size interacts with the core language: it can providestructured binding support in the tuple-like case.

      (2-4) are SFINAE-friendly: ifstd::tuple_size<T>::value isill-formed when treated as an unevaluated operand, they do not provide the membervalue. Access checking is performed as if in a context unrelated totuple_size andT. Only the validity of the immediate context of the expression is considered. This allows

      #include <utility> struct X{int a, b;};constauto[x, y]= X();// structured binding declaration first attempts// tuple_size<const X> which attempts to use tuple_size<X>::value,// then soft error encountered, binds to public data members
      (since C++17)

      Contents

      [edit]Specializations

      The standard library provides following specializations for standard library types:

      obtains the size of

      atuple
      (class template specialization)[edit]

      obtains the size of apair
      (class template specialization)[edit]
      obtains the size of anarray
      (class template specialization)[edit]
      obtains the size of astd::ranges::subrange
      (class template specialization)[edit]
      obtains the size of astd::complex
      (class template specialization)[edit]

      All specializations ofstd::tuple_size satisfyUnaryTypeTrait withbase characteristicstd::integral_constant<std::size_t, N> for someN.

      Users may specializestd::tuple_size for program-defined types to make them tuple-like. Program-defined specializations must meet the requirements above.

      Usually only specialization for cv-unqualified types are needed to be customized.

      [edit]Helper variable template

      Defined in header<tuple>
      template<class T>
      constexprstd::size_t tuple_size_v= tuple_size<T>::value;
      (since C++17)

      Inherited fromstd::integral_constant

      Member constants

      value
      [static]
      for a standard specialization, the number of elements in the tuple-like typeT
      (public static member constant)

      Member functions

      operator std::size_t
      converts the object tostd::size_t, returnsvalue
      (public member function)
      operator()
      (C++14)
      returnsvalue
      (public member function)

      Member types

      Type Definition
      value_typestd::size_t
      typestd::integral_constant<std::size_t, value>

      [edit]Example

      Run this code
      #include <array>#include <cstddef>#include <ranges>#include <tuple>#include <utility> template<class T,std::size_t Size>struct Arr{ T data[Size];}; // Program-defined specialization of std::tuple_size:template<class T,std::size_t Size>struct std::tuple_size<Arr<T, Size>>:public integral_constant<std::size_t, Size>{}; int main(){using tuple1=std::tuple<int,char,double>;    static_assert(3== std::tuple_size_v<tuple1>);// uses using template (C++17) using array3x4=std::array<std::array<int,3>,4>;    static_assert(4== std::tuple_size<array3x4>{});// uses operator std::size_t using pair=std::pair<tuple1, array3x4>;    static_assert(2== std::tuple_size<pair>());// uses operator() using sub= std::ranges::subrange<char*,char*>;    static_assert(2== std::tuple_size<sub>::value); using Arr5= Arr<int,5>;    static_assert(5== std::tuple_size_v<Arr5>);}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2212C++11specializations for cv types were not required in some headers, which led to ambiguityrequired

      [edit]See also

      Structured binding(C++17) binds the specified names to sub-objects or tuple elements of the initializer[edit]
      obtains the element types of a tuple-like type
      (class template)[edit]
      (C++11)
      creates atuple by concatenating any number of tuples
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/tuple_size&oldid=176453"

      [8]ページ先頭

      ©2009-2025 Movatter.jp