Reputation: 3219
I'm trying to achieve following: create template class which uses its template arguments to create instance of template type and use it somewhere in the, for example constructor. Consider following example:
template <typename T>
class foo
{
public:
explicit foo(const T& data) : m_data(data) {}
T m_data;
};
template <typename T01, typename T02>
class bar
{
public:
explicit bar(int data) : m_storage(T01(data), T02(data)) {}
void print() { boost::fusion::for_each(m_storage, printer()); }
private:
boost::fusion::vector<T01, T02> m_storage;
};
and usage:
bar<foo<int>, foo<int>> b(5);
b.print();
but what if I want flexibility in the bar
class and I want number of these T01, T02 classes varying?
example:
bar<foo<int>, foo<int>> b(5);
b.print();
bar<foo<int>, foo<int>>, foo<int>> c(6);
c.print();
Something like using argument pack maybe?
EDIT001:
Final working version on coliru.
Upvotes: 1
Views: 62
Reputation: 217075
You are looking for variadic template (available since C++11)
template <typename ... Ts>
class bar
{
public:
explicit bar(int data) : m_storage(Ts(data)...) {}
void print() { boost::fusion::for_each(m_storage, printer()); }
private:
boost::fusion::vector<Ts...> m_storage;
};
Upvotes: 4