是一个C++标准库中的函数模板,用于将参数转发给多个构造函数。它的作用是根据参数的值和类型,生成一个std::tuple对象,该对象可以作为参数传递给多个构造函数。 std::forward_as_...
#include <iostream> #include #include <tuple> #include <string> int main() { std::map<int, std::string> m; m.emplace(std::piecewise_construct, std::forward_as_tuple(10), std::forward_as_tuple(20, 'a')); std::cout << "m[10] = " << m[10] << '\n'; // 下面是错误...
所以它们是lvalue,如果没有std::forward,它们总是以这样的方式传递给std::forward_as_tuple。
#include <iostream>#include #include <tuple>#include <string>intmain(){std::map<int,std::string>m;m.emplace(std::piecewise_construct, std::forward_as_tuple(10), std::forward_as_tuple(20,'a'));std::cout<<"m[10] = "<<m[10]<<'\n';// The following is an error: it produces...
// produces std::tuple<int&, std::string&&> std::forward_as_tuple(x, std::string(L"hello")); Note that this potentially contains a tuple of rvalue references, which means that you had better use the tuple before the end of the statement, before the temporaries are destroyed. If you...
(6), std::forward_as_tuple(9, 'g')); std::cout << "m[6] = " << m[6] << '\n'; // 下面是错误:它产生保有两个悬垂引用的 std::tuple<int&&, char&&> // // auto t = std::forward_as_tuple(20, 'a'); // m.emplace(std::piecewise_construct, std::forward_as_tuple(...
std::move和std::forward只是执行转换的函数(确切的说应该是函数模板)。std::move无条件的将它的参数...