dimanche 21 août 2016

Template overload based on value of parameter

What I would like to do is to be able to have different declarations of the same template class, based on the value of a parameter, like this:

// Enable if X == 2
template <int X, int W, typename Y,
          typename A = int, typename B = int> struct Z {}; 

// Enable if X != 2
template <int X,        typename Y,
          typename A = int, typename B = int> struct Z {};

I could start with something like this:

template <int X, int W, typename Y, typename A = int, typename B = int, 
          typename = std::enable_if_t<X == 2>> struct Z {};
template <int X,        typename Y, typename A = int, typename B = int,
          typename = std::enable_if_t<X != 2>> struct Z {};

The problem with it is that, understandably, says that the it has been re-declared with a different number of parameters.

The variadic template feature could come in handy for this, but unfortunately, only supports types and not literals, as in this case.

template <typename... Args> struct Z {};
template <int X, int W, typename Y,
          typename A = int typename B = int> struct Z<X, W, Y> {};

type/value mismatch -> expected a type, got 'X/W'

Has anyone have a solution for this?

EDIT

Sorry I didn't mention earlier, but unfortunately I cannot change the order of the parameters, since other parameters after Y have default values.

Aucun commentaire:

Enregistrer un commentaire