This will not work with regular arrays because RangeType::const_iterator
will not be defined. It will also not work when passing to std::pair<iterator,iterator>
, which is also supported by Boost.Range.
Instead, you should use boost::range_iterator<const RangeType>::type
. This will work with all types supported by Boost.Range: normal iterable objects, arrays and pairs of iterators.
Example:
template <typename RangeType> void DoSomethingWithRange(const RangeType &range) { typedef typename boost::range_iterator<const RangeType>::type const_iterator; const_iterator endIt = boost::end(range); for(const_iterator it = boost::begin(range); it != endIt; ++it) cout << *it << endl; } int main(int, char** ) { vector<int> test; test.push_back(1); test.push_back(-1); DoSomethingWithRange(test); int test2[] = {12,34}; DoSomethingWithRange(test2); std::pair<int*,int*> test3(test2, test2+1); DoSomethingWithRange(test3); }
source share