Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::piecewise_construct,std::piecewise_construct_t

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
       
      Defined in header<utility>
      struct piecewise_construct_t{explicit piecewise_construct_t()=default;};
      (1)(since C++11)
      constexprstd::piecewise_construct_t piecewise_construct{};
      (2)(since C++11)
      (inline since C++17)
      1)std::piecewise_construct_t is an empty class tag type used to disambiguate between different functions that take two tuple arguments.
      2) The constantstd::piecewise_construct is an instance of(1).

      The overloads that do not usestd::piecewise_construct_t assume that each tuple argument becomes the element of a pair. The overloads that usestd::piecewise_construct_t assume that each tuple argument is used to construct, piecewise, a new object of specified type, which will become the element of the pair.

      Contents

      [edit]Standard library

      The following standard library types and functions use it as a disambiguation tag:

      implements binary tuple, i.e. a pair of values
      (class template)[edit]
      prepares the argument list matching the flavor of uses-allocator construction required by the given type
      (function template)[edit]
      aview consisting of a generated sequence by repeatedly producing the same value
      (class template)(customization point object)[edit]

      [edit]Example

      [edit]
      Run this code
      #include <iostream>#include <tuple>#include <utility> struct Foo{    Foo(std::tuple<int,float>){std::cout<<"Constructed a Foo from a tuple\n";}     Foo(int,float){std::cout<<"Constructed a Foo from an int and a float\n";}}; int main(){std::tuple<int,float> t(1,3.14); std::cout<<"Creating p1...\n";std::pair<Foo, Foo> p1(t, t); std::cout<<"Creating p2...\n";std::pair<Foo, Foo> p2(std::piecewise_construct, t, t);}

      Output:

      Creating p1...Constructed a Foo from a tupleConstructed a Foo from a tupleCreating p2...Constructed a Foo from an int and a floatConstructed a Foo from an int and a float

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2510C++11the default constructor was non-explicit, which could lead to ambiguitymade explicit

      [edit]See also

      constructs new pair
      (public member function ofstd::pair<T1,T2>)
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/piecewise_construct&oldid=173771"

      [8]ページ先頭

      ©2009-2025 Movatter.jp