C++ Utility 库 - piecewise_construct 函数
描述
它是一个分段构造常量,并且该常量值作为第一个参数传递以构造一个对对象,以通过将两个元组对象的元素转发给它们各自的构造函数来选择构造其成员的构造函数形式。
声明
以下是 std::piecewise_construct 函数的声明。
constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
C++11
constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
参数
none
返回值
none
异常
none
数据竞争
none
示例
在下面的示例中解释了 std::piecewise_construct 函数。
#include <utility>
#include <iostream>
#include <tuple>
#include <vector>
#include <string>
int main () {
std::pair < std::string, std::vector<int> >
foo (
std::piecewise_construct,
std::forward_as_tuple("sample"),
std::forward_as_tuple(2,100)
);
std::cout << "foo.first: " << foo.first << '\n';
std::cout << "foo.second:";
for (int& x: foo.second) std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
foo.first: sample foo.second: 100 100

