In C++, is there a way to build a compile time list incrementally, in the following pattern?
START_LIST(List)
ADD_TO_LIST(List, int)
ADD_TO_LIST(List, float)
You could use C++11 variadic templates directly, they allow to write typelists in a much more fancy way than the classic functional head:tail
approach:
template
struct list{};
using l = list;
On the other hand, if you like the head-tail way you could translate from one format to the other. In this case (Variadic to functional):
template
struct list{};
struct nil{};
template
struct make_list;
template
struct make_list
{
using result = list::result;
};
template<>
struct make_list<>
{
using result = nil;
};
An example:
//l is list>>
using l = typename make_list::result;
Of course you could use template aliases to make the syntax more clear:
template
using make = typename make_list::result;
using l = make;