I would like to pass one lvalue to a function that expects a pair of iterators, and so that it acts as if I passed a pair of iterators to a range containing only this value.
My approach is as follows:
#include <iostream> #include <vector> template<typename Iter> void iterate_over(Iter begin, Iter end){ for(auto i = begin; i != end; ++i){ std::cout << *i << std::endl; } } int main(){ std::vector<int> a{1,2,3,4}; iterate_over(a.cbegin(), a.cend()); int b = 5; iterate_over(&b, std::next(&b)); }
It seems to work correctly in g ++ 5.2, but I wonder if this behavior is really defined and are there any potential problems?
source share