C++17新增了make_from_tuple函数
template< class T, class Tuple >
constexpr T make_from_tuple( Tuple&& t );
该函数用于通过tuple构造出一个对象:
#include <iostream>
#include <tuple>
#include <string>
using namespace std;
class A{
public:
A(string p1, int p2):m_p1(p1), m_p2(p2){}
void pOut()
{
cout<<m_p1<<" "<<m_p2<<endl;
}
private:
string m_p1;
int m_p2;
};
int main()
{
tuple t = {"hi", 88};
A&& a = make_from_tuple<A>(t);
a.pOut();
return 0;
}
运行程序输出:
hi 88