c++templatesconditional-statementsopaque-pointers

Is it possible to exclude template member variables by conditions on template arguments?


I am currently writing a class which should specialize depending on a template argument.

I am now wondering whether it is possible to leave out certain member variables for specific specializations to be more exact depending on whether a numeric template argument is larger than X.

So for example:

template<int N>
class Test
{
   int a;
   int b;
}

template<N > X>
class Test
{
  int a;
}

I was thinking about the use of std::conditional but that seems to result in at least one type being picked. I could of course the D-Pointer method and std::conditional and put the specialization into different objects that are pointed at but I wondered whether there was a better way.

An alternative I see is to use an abstract base class and have two implementations one for N < X and one for N >= X but I'm not sure that this would be better.


Solution

  • Just use SFINAE.

    template<int N, typename = void>
    class Test
    {
       int a;
       int b;
    };
    
    template<int N>
    class Test<N, typename std::enable_if<(N > X)>::type>
    {
      int a;
    };