【发布时间】:2017-04-18 14:26:53
【问题描述】:
我想使用 std::tuple 的所有元素作为类的初始化程序。有没有比为元组的每个元素做std::get<i-th element>(std::tuple) 更简单的方法?
std::get 的最小工作示例:
#include <string>
#include <tuple>
#include <cassert>
struct A
{
std::string string1;
int intVal;
std::string string2;
};
int main()
{
std::tuple< std::string, int, std::string > myTuple("S1", 42, "S2");
A myA{ std::get<0>(myTuple), std::get<1>(myTuple), std::get<2>(myTuple) };
assert( myA.string1 == "S1" );
assert( myA.intVal == 42 );
assert( myA.string2 == "S2" );
}
查看http://coliru.stacked-crooked.com/a/4a5d45dbf1461407 了解实时示例
【问题讨论】:
-
例如见P0209。