Multiple templating templates for template class
Is it possible to pass two template classes to a template class?
I want to create a class that contains two different ones std::tuple<std::vector<>>
.
I am beginning to suspect that what I want to achieve cannot be done, but I cannot 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 an alternative?
source to share
This is what I think you can keep in mind.
#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;
}
Changes made:
First, the specialization of the template has been changed to two 6-parameter packages, this is doable since the parameter packages are populated with a type-based pattern matching engine with Typelist
, so it is unambiguous. You cannot use the spec you used earlier because then you only have access to T1
and T2
, you do not have access to the names of the internal arguments of the parameter package.
Secondly, I changed the way the data items are defined World
so that m1
they m2
are tuples of vectors of types.
Third, you can get rid of completely Typelist
and use tuple
directly. Unless it does something not shown 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;
}
source to share