在线文档教程
C++
应用 | Utilities

std::piecewise_construct_t

STD:分段[医]构造[医]T型

Defined in header
struct piecewise_construct_t { };(since C++11) (until C++17)
struct piecewise_construct_t { explicit piecewise_construct_t() = default; };(since C++17)

std::piecewise_construct_t是一个空的struct标记类型,用于消除使用两个元组参数的不同函数之间的歧义。

不使用的重载std::piecewise_construct_t假设每个元组参数都成为一对的元素。使用的过载std::piecewise_construct_t假设每个元组参数都用于构造一个指定类型的新对象,该对象将成为该对的元素。

二次

#include <iostream> #include <utility> #include <tuple> 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::pair<Foo, Foo> p1(t, t std::pair<Foo, Foo> p2(std::piecewise_construct, t, t }

二次

产出:

二次

Constructed a Foo from a tuple Constructed a Foo from a tuple Constructed a Foo from an int and a float Constructed a Foo from an int and a float

二次

另见

piecewise_construct (C++11)an object of type piecewise_construct_t used to disambiguate functions for piecewise construction (constant)
(constructor)constructs new pair (public member function of std::pair)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/实用程序/分段[医]构造[医]T型