staticFindInRange should return something [suggestion]
Currently the staticFindInRange<begin,end>(predicate)
loop iterates over indices in [begin, end)
and terminates if the predicate returns true. This seems to be a bit similar to the std::find_if
method, with begin and end the corresponding begin/end-iterators.
The only difference is, that std::find_if
returns an iterator to the found element, or the end-iterator if not found. I would suggest to extend staticFindInRange
in a similar fashion. Either just return true/false, if terminated by positive predicate, or return the found iterator, aka the corresponding index:
template<class ST, ST begin, ST end>
struct StaticFindInRangeImpl<ST, begin, end, true>
{
template<class F, class...Args>
static ST apply(F&& f, Args&&... args)
{
if (f(std::integral_constant<ST, begin>(), std::forward<Args>(args)...))
return begin;
return StaticFindInRange<ST, begin+1, end>::apply(std::forward<F>(f), std::forward<Args>(args)...);
}
};
template<class ST, ST begin, ST end>
struct StaticFindInRangeImpl<ST, begin, end, false>
{
template<class F, class...Args>
static ST apply(F&& f, Args&&...)
{
return end;
}
};
One could workaround this, by adding a second argument to the predicate that is set to the index, when the predicate returns true, but this is simply not a nice interface to use.
Instead of returning ST
the return value bool
would also be nice to use instead. It just returns true for early termination, and false otherwise. What is better?
Another extensions of staticFindInRange
is a generalized break condition, i.e. if begin >= end
:
template<class ST, ST begin, ST end, bool condition>
struct StaticFindInRangeImpl;
// terminate if (begin >= end)
template<class ST, ST begin, ST end>
using StaticFindInRange =
StaticFindInRangeImpl<ST, begin, end, (begin < end)>;