std::piecewise_construct_t

来自cppreference.com
< cpp‎ | utility
定义于头文件 <utility>
struct piecewise_construct_t { };
(C++11 起)
(C++17 前)
struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
(C++17 起)

std::piecewise_construct_t 是用于在接收二个 tuple 参数的不同函数间消歧义的空结构体标签类型。

不使用 std::piecewise_construct_t 的重载假设每个 tuple 参数各变成一个 pair 的元素。使用 std::piecewise_construct_t 的重载假设每个 tuple 参数用于逐块构造一个指定类型的新对象,而它将成为 pair 的元素。

[编辑] 示例

#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_t 类型的对象
(常量) [编辑]
构造新的 pair
(std::pair 的公开成员函数)