Search code examples
c++templatestemplate-specializationpartial-specialization

How can this condition be put in template partial specialization?


template<size_t bits_count, typename = void>
struct best_type {
};

template<size_t bits_count>
struct best_type<bits_count,enable_if_t<bits_count > 8>> { // error: template argument 2 is invalid
  typedef byte type;
};

The error is because of the parser sees the second template argument as enable_if_t<bits_count > following a random 8.

Obviously the solution to this can be replacing the argument of enable_if_t to bits_count >= 9, but can something be done to preserve the original expression so it will make sense to future readers?


Solution

  • You should add additional parentheses to explain the compiler what you mean:

    template<size_t bits_count>
    struct best_type<bits_count,enable_if_t<(bits_count > 8)>> {
        typedef byte type;
    };