samedi 6 janvier 2018

Why can't we use variadic template at the beginning/middle of template specialization (and how to emulate)?

I was wondering how to emulate the following:

template<class> struct foo; 

template<template<class..., int..., class...> class list,
class... clist1, int... ilist1, class... clist2>
struct foo<list<clist1..., ilist1..., clist2...>> {

};

This specialization (hypothetically) accepts a class with any number of classes, then integers, then classes. Currently there seems to be no way to directly implement this. (Bonus points if you can comment on why the standard doesn't support this)

An example of when this would be practical is in Eigen's linear algebra library where most classes are structures like:

Vector<class Scalar_type, int rows, class... options>
Mstrix<class Scalar_type, int rows, int cols, class... options>

With the above enabled, meta-template functions could be used that accept any of these base classes. Because it is not supported you have to write specializations for each class.

Is there any way to emulate a template function that supports accepting classes like the above?

I am looking for a generic solution. (Writing specializations for each class is not generic, and I am not directly looking for a solution to Eigen's library, that is just an example)

Aucun commentaire:

Enregistrer un commentaire