I have a sort with the following interface:
template< class RandomIt >
void my_sort( RandomIt first, RandomIt last )
{
}
I expect RandomIt to be an iterator from std::vector<T>.begin()/end() or a plain pointer-type T* first,T* last.  I think if I assume RandomIt is a vector, I can get it from RandomIt::value_type, but then this will not work for T* first,T* last.
My question is, how can I extract the value_type T from the template parameter in both cases?
Use iterator_traits<T>::value_type (cppreference). Note that the standard library provides iterator_traits definitions for T* and const T*, so it also works on plain pointers.
Since you're using C++11, you can apply decltype on the iterator itself to get the value_type:
typedef decltype(*first) value_type;
Note iterator_traits may not work for programmer-defined types if the programmer doesn't specialize iterator_traits for his iterators, or he doesn't define iterator meeting Standard requirements. 
However, the decltype trick will work even then.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With