std::p iecewise_construct语法是如何工作的

How does the std::piecewise_construct syntax work?

本文关键字:何工作 工作 iecewise construct 语法 std      更新时间:2023-10-16

当与std::map一起使用时,我对std::piecewise_construct有点困惑。例:

std::map<std::string, std::string> m;
// uses pair's piecewise constructor
m.emplace(std::piecewise_construct,
          std::forward_as_tuple("c"),
          std::forward_as_tuple(10, 'c'));

我不确定emplace()在使用piecewise_construct时如何知道如何以不同的方式处理这种类型的结构。不应该是:std::piecewise_construct(std::forward_as_tuple("c"), std::forward_as_tuple(10, 'c'))?它如何仅使用逗号,我没有看到重载的逗号运算符或特殊的重载emplace来处理分段然后是变量参数(如此处所示)。

std::map::emplace直接调用构造函数,并将参数emplace传递给类型std::map<std::string, std::string>::value_type(转发它们)(这是 typedef 到 std::pair<const std::string, std::string> )。

std::pair构造函数采用std::piecewise_construct_tstd::piecewise_construct的类型)

map::emplace只是将其参数转发给pair<const K, V>的构造函数。 Pair 有一个构造函数重载,它将piecewise_construct作为第一个参数。

查看构造函数 #6 http://en.cppreference.com/w/cpp/utility/pair/pair