Question

template <typename _Type, typename _Comparator=less<typename _Type::value_type> >
class Myclass
// and then comes the rest of my class

I want to create a specialized template with strings (but also working with other comparator than the default one) I tried this but it is not compiling.

template <typename _Comparator=less<typename _Type::value_type> >
class Myclass<string>

I know that the next example works but it uses the default comparator I fixed.

template <>
class Myclass<string>
Était-ce utile?

La solution

It is not allowed to specify default template arguments in the class template specialization. Use the following syntax to achieve what you want:

template <typename Comparator>
class Myclass<string, Comparator>
{
    // ...
};

Autres conseils

You can make another default template parameter:

template<class Comparator = other_functor>
class Myclass<string, Comparator> {

};

Remove the =less<typename _Type::value_type> when you declare the specialization.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top