它的作用是根据参数的值和类型,生成一个std::tuple对象,该对象可以作为参数传递给多个构造函数。 std::forward_as_tuple的使用方式如下: 代码语言:cpp 复制 #include <tuple> #include <iostream> struct MyClass { int a; double b; std::string c; MyClass(int a, double b, std::string c...
所以它们是lvalue,如果没有std::forward,它们总是以这样的方式传递给std::forward_as_tuple。
若参数是临时量,则 forward_as_tuple 不延续其生存期;必须在完整表达式结尾前使用它们。 示例运行此代码 #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...
tuple<int,bool, string, Person> t2 =tuple<int,bool, string, Person>(11,true,"ok", p); } { cout <<"---//(3)"<< endl; tuple<int,bool, string, Person> t3 = std::forward_as_tuple(11,true,"ok",Person("ok",11)); } { cout <<"---//(4)"<< endl;Personp("ok",11);...
#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...
若实参是临时量,则 forward_as_tuple 不延续其生存期;必须在完整表达式结尾前使用它们。 示例运行此代码 #include <iostream> #include #include <string> #include <tuple> int main() { std::map<int, std::string> m; m.emplace(std::piecewise_construct, std::forward_as_tuple(6), std::forward...
forward_as_tupleis to wrap up a bunch of parameters, probably a parameter pack, into a single object that can be passed as a single non-pack parameter. This is handy if you need to work with a pattern that accepts only a single parameter, so you use the tuple as a way to aggregate...
{// std::forward_as_tuple: function template, Constructs a tuple object with rvalue references// to the elements in args suitable to be forwarded as argument to a function.std::stringstr("John");print_pack(std::forward_as_tuple(str +" Smith",25));print_pack(std::forward_as_tuple(st...
std::map<std::string, std::string> m // emplace的原地构造需要使用std::piecewise_construct,因为是直接插入std::pair<key, value> m.emplace(std::piecewise_construct, std::forward_as_tuple("c"), std::forward_as_tuple(10, &aposc&apos)) // try_emplace可以直接原地构造,因为参数列表...
// The constructor uses the same recursion as the inheritance template <typename... CArgs> tuple(CArgs &&... args) : _tuple_recurr_base<0, L, types...>(std::forward<CArgs>(args)...) { } }; 具体使用如下: tuple<int, double, float> t; ...