0

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.

Blackclaws
  • 436
  • 5
  • 20
  • What's exact problem with `std::conditional`? – Petr Nov 30 '15 at 14:24
  • This seems a bit like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you trying to do with this model? – TartanLlama Nov 30 '15 at 14:26
  • 1
    I'm trying to select between two or more different methods of implementing a class based on what would be more efficient depending on the requirements to the class. So to say method X would be more efficient to use when N is smaller than 4 and method Y would be more efficient to use otherwise. However the methods have different member variable requirements. – Blackclaws Nov 30 '15 at 14:33

1 Answers1

4

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;
};
Nic
  • 6,211
  • 10
  • 46
  • 69
ForEveR
  • 55,233
  • 2
  • 119
  • 133