1

Is it a good practice to declare a template class, but only with specialized implementations ?

Ie, there are many CRC flavours (see https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Polynomial_representations), but not a generic one.
It could make sense to declare a CRC class with two template parameters : the CRC size, and a policy class, as :

template<Size, Flavour>
class CRC;

template<>
CRC<16,IBM>
{
...
};

I could have a class CRC16IBM, but it appears to me less readable than CRC<16,IBM>. Also, I guess I could factorize some code for all CRC implementations on 16 bits.

1
  • 1
    You can also create an alias for it. using CRC16IBM = CRC<16, IBM>;. It is common-place in the STL (e.g., using string = basic_string<char>;)
    – Rish
    Commented Apr 10 at 6:17

1 Answer 1

4

Is it a good practice to declare a template class, but only with specialized implementations ?

That should not be a problem. The C++ standard even has it in several places, like std::numeric_limits<>.

1
  • 1
    Quite a few places, iirc all of the std::foo_traits<> templates are only complete types in specialisations
    – Caleth
    Commented Apr 9 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.