Implementing various classes based on a template parameter

I suppose this is trivial for people who know patterns ...

Suppose we need two different implementations of this template, depending on the value of N:

template <int N> class Foo { ... }; 

For instance:

 template <int N> class Foo { ... // implementation for N <= 10 }; template <int N> class Foo { ... // implementation for N > 10 }; 

How can we do this in C ++ 11?

+6
source share
1 answer

Use the optional template parameter with a default value to distinguish between cases:

 template <int N, bool b = N <= 10> class Foo; template <int N> class Foo<N, true> { ... // implementation for N <= 10 }; template <int N> class Foo<N, false> { ... // implementation for N > 10 }; 
+19
source

Source: https://habr.com/ru/post/971536/


All Articles