std::piecewise_construct

来自cppreference.com
< cpp‎ | utility
 
 
 
 
constexpr piecewise_construct_t piecewise_construct
    = std::piecewise_construct_t();
(C++11 起)
(C++17 前)
inline constexpr piecewise_construct_t piecewise_construct
    = std::piecewise_construct_t();
(C++17 起)

常量 std::piecewise_construct 是空的结构体标签类型 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

参阅

用于为逐段构造选择正确函数重载的标签类型
(类)
构造新的 pair
(std::pair<T1,T2> 的公开成员函数)