Is it possible to pass two template classes into a template class?
I am looking to create a class that holds two different std::tuple<std::vector<>>
.
I am beginning to suspect that what I want to achieve can not be done, but I can not find anything that says otherwise.
Below is the code I am working with:
#include <iostream>
#include <vector>
#include <tuple>
template<typename... Ts>
struct Typelist
{
static constexpr std::size_t size { sizeof...(Ts) };
};
template<class, class> class World;
template<template<typename... Arg1> class T1, template<typename... Arg2> class T2>
class World<Typelist, Typelist>
{
private:
std::tuple<std::vector<T1>...> m1;
std::tuple<std::vector<T2>...> m2;
};
int main() {
// your code goes here
using TL1 = Typelist<int, char, double>;
using TL2 = Typelist<float, unsigned int, bool>;
World<TL1, TL2> w2;
return 0;
}
Is this possible, and if so, what am I doing wrong? If not, is there a possible alternative?
This is what I think you might mean.
#include <iostream>
#include <vector>
#include <tuple>
template<typename... Ts>
struct Typelist
{
static constexpr std::size_t size { sizeof...(Ts) };
};
template <class, class>
class World;
template <typename... Arg1, typename... Arg2>
class World<Typelist<Arg1...>, Typelist<Arg2...>>
{
private:
std::tuple<std::vector<Arg1>...> m1;
std::tuple<std::vector<Arg2>...> m2;
};
int main() {
using TL1 = Typelist<int, char, double>;
using TL2 = Typelist<float, unsigned int, bool>;
World<TL1, TL2> w2;
return 0;
}
The changes made:
First, the template specialization was changed to two bare parameter packs, this is doable, because the parameter packs are filled by the template matching engine based on the types with the Typelist
, so it is unambiguous. You cannot use the specification you used previously because then you only have access to T1
and T2
, you do not have access to the names of the inner parameter pack arguments.
Second, I changed how the data members of World
were defined, so that m1
and m2
are tuples of vectors of types.
Third, you can get rid of Typelist
entirely and use a tuple
directly. Unless it's doing something not featured in this code.
#include <iostream>
#include <vector>
#include <tuple>
template <class, class>
class World;
template <typename... Arg1, typename... Arg2>
class World<std::tuple<Arg1...>, std::tuple<Arg2...>>
{
private:
std::tuple<std::vector<Arg1>...> m1;
std::tuple<std::vector<Arg2>...> m2;
};
int main() {
using TL1 = std::tuple<int, char, double>;
using TL2 = std::tuple<float, unsigned int, bool>;
World<TL1, TL2> w2;
return 0;
}