Tomas Chalupnik
Tomas Chalupnik

Reputation: 115

Passing "value type" as default second argument to template

I am writing a template for class which takes as 1st argument some STL container (string,vector,list) and 2nd argument is operator< by default. I figured out that in is less so I tried to implement it. Problem is that I am not able to get T2 which should be the T's "value type" (string -> char,vector -> T, list -> T)

template <typename T, typename C = less<T2> > // using T as T2 leads to error in
                                              // conversion from 'char' to 'const char *'
                                              // (for string as T)
class MyClass
{
  ...
   public:
       CIndex ( const T& x, const C& comp = C ()) {}
  ...
}

What is solution to thi? To be argument of less dependent on type T? Thank you very much for your help, I am not much experienced with templates

Upvotes: 1

Views: 95

Answers (1)

juanchopanza
juanchopanza

Reputation: 227418

Use the container's value_type:

template <typename T, typename C = less<typename T::value_type> >

Upvotes: 3

Related Questions