Use partial specialization and inheritance:
// Factor common code in a base class template <size_t n, size_t m> class MyClassTBase { // Put here the methods which must appear // in MyClassT independantly of n, m }; // General case: no extra methods template <size_t n, size_t m> class MyClassT : MyClassTBase<n, m> {}; // Special case: one extra method (you can add more here) template <size_t n> class MyClassT<n, n> : MyClassTBase<n, n> { static MyClassT<n, n> SomeFunc() { ... } };
Another option is to use SFINAE: std::enable_if or its variant:
template <size_t n, size_t m> class MyClassT { template <typename EnableIf = char> static MyClassT<n, m> SomeFunc(EnableIf (*)[n == m] = 0) { ... } };
more sophisticated alternative (but less surprising if you don't know about SFINAE and pointer to arrays)
template <size_t n, size_t m> class MyClassT { template <typename Dummy = char> static MyClassT<n, m> SomeFunc(typename std::enable_if<n == m, Dummy>::type * = 0) { ... } };
Generally, I prefer SFINAE approaches where there is one or two member functions to enable or disable. Once it becomes more complex, I prefer the partial specialization method.
EDIT: The SFINAE code was incorrect because the template functions were not. Fixed.
source share