C ++ 11 does not have a range-based loop for a coherent sequence.
for(auto e : {0..10} )
So, I just decided to imitate it.
template< class T , bool enable = std::is_integral<T>::value > struct range_impl { struct iterator { constexpr T operator * ()const noexcept { return value; } iterator& operator ++()noexcept { ++value; return *this; } friend constexpr bool operator != (const iterator & lhs, const iterator rhs ) noexcept { return lhs.value != rhs.value; } T value; }; constexpr iterator begin()const noexcept { return { first }; } constexpr iterator end ()const noexcept { return { last }; } T first; T last ; }; template< class T > range_impl<T> range(T first , T last) noexcept { return {first, last}; } int main(){
Q: How to generalize this method to ForwardIterators?
Example:
template< class ForwardIterator, class T > bool find(ForwardIterator first, ForwardIterator last, T const& value) { for(auto e: range(first, last) ) if (e == v) return true; return false; }
c ++ for-loop c ++ 11
Khurshid Normuradov
source share