In a C++ template, I struggle to define a variable with the correct size_type
. Basically, this will be an index type into the container. I know that int
works but would like to have it in a clean form.
template<typename ForwardIt>
void test(ForwardIt it) {
// this yields the underlying type
decltype(typename std::iterator_traits<ForwardIt>::value_type()) T;
// what I would like is something like above, but yielding the
// correct size_type for the container.
// Something like this but with the type extracted from ForwardIt:
std::vector<int>::size_type i;
...
}
Elaborating on what @NathanOliver said: Iterators don't have size_type
s; they have a difference_type
, which represents the distance between two iterators. Containers have a size_type
.
An iterator need not have an associated container, so there's no way to get "the container's size_type
" from just an iterator.