若参数是临时量,则 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...
最后,forward_as_tuple生成一个引用元组,但是引用的东西仍然必须存储在 * 某个地方 *。
所以它们是lvalue,如果没有std::forward,它们总是以这样的方式传递给std::forward_as_tuple。
最后,forward_as_tuple生成一个引用元组,但是引用的东西仍然必须存储在 * 某个地方 *。
若实参是临时量,则 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...
如果你想以元组的形式储存东西,你可以。只需在lambda中捕获元组,然后使用std::get在lambda的主体中访问...
We’ll start withstd::forward_as_tuple: This takes its arguments and produces a tuple of corresponding references. int x; // 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, ...
#include<tuple>#include<iostream>std::tuple<float,std::string>get_values(){floata=0.01;std::string b="TutorialsPoint";returnstd::forward_as_tuple(a,b);}intmain(){autox=get_values();std::cout<<std::get<0>(x)<<", "<<std::get<1>(x)<<std::endl;return0;} ...
我正在使用C ++ 17和Visual C ++ 2017,我正在使用std::tuple创建一个std::forward_as_tuple的引用。 从C ++ 14开始,可以通过使用类类型而不是索引来访问元组的元素。 当我尝试下面的代码时,我有编译错误 error C2338: duplicate type T in get<T>(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...