Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::make_from_tuple

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
      Defined in header<tuple>
      template<class T,class Tuple>
      constexpr T make_from_tuple( Tuple&& t);
      (since C++17)
      (until C++23)
      template<class T, tuple-like Tuple>
      constexpr T make_from_tuple( Tuple&& t);
      (since C++23)

      Construct an object of typeT, using the elements of the tuplet as the arguments to the constructor.

      Given the exposition-only function/*make-from-tuple-impl*/ defined as follows:
      template<class T, tuple-like Tuple,std::size_t...I>// no constraint on Tuple before C++23
      constexpr T/*make-from-tuple-impl*/(Tuple&& t,std::index_sequence<I...>)
      {
          return T(std::get<I>(std::forward<Tuple>(t))...);
      }

      The effect is equivalent to:
      return/*make-from-tuple-impl*/<T>(
         std::forward<Tuple>(t),
         std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>{}
      );
      .

      If

      (since C++23)

      the program is ill-formed.

      Contents

      [edit]Parameters

      t - tuple whose elements to be used as arguments to the constructor ofT

      [edit]Return value

      The constructedT object or reference.

      [edit]Notes

      Tuple need not bestd::tuple, and instead may be anything that supportsstd::get andstd::tuple_size; in particular,std::array andstd::pair may be used.

      (until C++23)

      Tuple is constrained to be tuple-like, i.e. each type therein is required to be a specialization ofstd::tuple or another type (such asstd::array andstd::pair) that modelstuple-like.

      (since C++23)

      Due toguaranteed copy elision,T need not be movable.

      Feature-test macroValueStdFeature
      __cpp_lib_make_from_tuple201606L(C++17)std::make_from_tuple

      [edit]Example

      Run this code
      #include <iostream>#include <tuple> struct Foo{    Foo(int first,float second,int third){std::cout<< first<<", "<< second<<", "<< third<<'\n';}}; int main(){auto tuple=std::make_tuple(42,3.14f,0);    std::make_from_tuple<Foo>(std::move(tuple));}

      Output:

      42, 3.14, 0

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3528C++17cast containingreinterpret_cast etc. was allowed in the case of 1-tupleprohibited

      [edit]See also

      (C++11)
      creates atuple object of the type defined by the argument types
      (function template)[edit]
      creates atuple offorwarding references
      (function template)[edit]
      (C++17)
      calls a function with a tuple of arguments
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/make_from_tuple&oldid=157348"

      [8]ページ先頭

      ©2009-2025 Movatter.jp