I am trying to achieve the following using c++ template metaprogramming. I wish to build up a list of types and then collect these types together and do further compile-time
A solution utilizing a common header, variadic templates and a macro:
// Header common.h
// A distinct Void type
struct Void {};
template struct concat;
template class List, typename T>
struct concat, T>
{
typedef List type;
};
template class List, typename ...Types, typename T>
struct concat, T>
{
typedef List type;
};
template struct TypeList {};
template <>
struct TypeList {};
typedef TypeList TypelistVoid;
#define TYPE_LIST TypelistVoid
// Header foo.h
#include
class Foo { };
typedef typename concat::type TypeListFoo;
#undef TYPE_LIST
#define TYPE_LIST TypeListFoo
// Header bar.h
#include
class Bar { };
typedef typename concat::type TypeListBar;
#undef TYPE_LIST
#define TYPE_LIST TypeListBar
// Header main.h
#include "foo.h"
#include "bar.h"
struct list_of_types {
typedef TYPE_LIST type;
};
// Or just typedef TYPE_LIST list_of_types;
// Test
#include
#include
template class List, typename T, typename ...Types>
void info();
template
inline void info(TypeList) {
std::cout << typeid(T).name() << std::endl;
info(TypeList());
}
template
inline void info(TypeList) {
std::cout << typeid(T).name() << std::endl;
}
int main() {
info(list_of_types::type());
return 0;
}