Using auto
is the easiest approach using C++11. You can also use decltype()
, of course:
decltype(*itrOuter) tmp = *iterOuter;
which will, as your approach using auto
, copy the value. If you actually want to get a reference you'd use
decltype((*itrOuter)) tmp = *iterOuter;
which is roughly equivalent to:
auto& tmp = *iterOuter;
Assuming you need to use C++03, there is no way to deduce the variable type in this location. The only approach deducing the variable type is to delegate to a function template. The normal C++03 approach is to obtain the value_type
from std::iterator_traits<...>
:
typename std::iterator_traits<RandomAccessIterator>::value_type tmp = *iterOuter;
... or, doing the same with a reference:
typename std::iterator_traits<RandomAccessIterator>::reference tmp = *iterOuter;