jeudi 26 mars 2015

Making a tuple-like compile-time "linked-list" with variadic templates

I was pondering possible implementations of std::tuple (and any similar template classes with a variable number of "members" defined at compile time), and I thought perhaps one could create a "recursive type" resembling a linked list. I tried compiling the following test-case:



template <typename FirstType, typename... OtherTypes>
class TupleLite
{
public:
FirstType type_;
TupleLite<OtherTypes...> other_types_;
};

int main()
{
TupleLite<int,double> mytuple;
}


The class itself compiles without error, but the instantiation throws the error wrong number of template arguments (0, should be 1 or more). I believe this is because TupleLite<int, double> tries to instantiate a TupleLite<double>, which tries to instantiate a TupleLite<>, for which there is no valid definition.


Can this "recursively sized class" be salvaged? I tried defining the "no-argument specialization" of TupleLite as follows:



template <>
class TupleLite {}


....but that's not actually a legal specialization.


Aucun commentaire:

Enregistrer un commentaire