std::piecewise_construct

来自cppreference.com
< cpp‎ | utility
 
 
工具库
通用工具
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)(C++20)(C++20)
(C++20)
swap 与类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

初等字符串转换
(C++17)
(C++17)
栈踪
 
std::pair
成员函数
非成员函数
(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20)
(C++11)
推导指引(C++17)
辅助类
(C++11)
 
定义于头文件 <utility>
constexpr std::piecewise_construct_t piecewise_construct{};
(C++11 起)
(C++17 前)
inline constexpr std::piecewise_construct_t piecewise_construct{};
(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> 的公开成员函数)