My question is sure a simple one for anybody familiar with C++ syntax. I’m learning C++ and this is some sort of homework.
template<typename Iter>
void quickSort(Iter begin, Iter end)
{
//..
auto pivot = * ( begin + (end - begin)/2 );
//..
}
pivot is supposed to contain the value from the center of the interval [begin, end].
The code I wrote there works, but auto is a keyword from the new C++11 language standard. How to do it the old-way? What do I write instead of auto?
typename std::iterator_traits<Iter>::value_typeThis will work if your template is instantiated with
Iteras a pointer type.By the way,
typenameisn’t part of the type itself. It tells the compiler thatvalue_typereally is a type. If it were the name of a function or a static data member, then that affects the syntax. The compiler doesn’t necessarily know what it is, since the specialization ofiterator_traitsforItermight not be visible when the template is compiled.