有的時候我們需要在構建對象時注入一系列類型不確定的對象或插件,怎么才能實現呢?
#include <iostream>
#include <string>
#include <tuple>using namespace std;class A{
public:A(int a) : m_a(a){cout<<"construct A:"<<m_a<<endl;}int m_a;
};class B{
public:B(string s) : m_s(s){cout<<"construct B:"<<m_s<<endl;}string m_s;
};class C{
public:C(){cout<<"construct C"<<endl;}double m_d{3.14};
};template<typename T, typename Tuple>
struct has_type;template<typename T, typename U, typename... Ts> //如果tuple的第一個類型U不是T,彈出U,遞歸調用has_type。比如T的類型是B時, Ts是<A, B>,所以先解包Ts為A和Ts=<B>,遞歸調用has_type
struct has_type<T, tuple<U, Ts...>> : has_type<T, tuple<Ts...>>
{
};template<typename T, typename... Ts>