vendredi 3 mai 2019

Mixing aliases and template specializations

I'm trying to find the best method to have a kind of "object" that can be either specialized or "linked" to another type.

For instance you cannot specialize a class to make it become a simple int,and you cannot use the keyword using to specialize classes.

My solution is the following:

template<class Category, Category code>
struct AImpl
{ };

template<class Category, Category code>
struct AHelper
{
    using type = AImpl<Category, code>;
};

template<class Category, Category code>
using A = typename AHelper<Category, code>::type;

template<int code>
void doSomething(A<int, code> object)
{
}

This works but you need to specify the code parameter in doSomething, and I would like to avoid that.

For instance:

A<int, 5> a1;
doSomething(a1); // This does not compile
doSomething<5>(a1); // This compiles but is bulky

Is there a way to achieve what I want? it's ok to change both doSomething or the A implementation.

Aucun commentaire:

Enregistrer un commentaire