While attempting to answer a question by Mehrdad, I concocted the little function below (in action at liveworkspace):
template <typename T, unsigned low, unsigned high>
static constexpr auto highest_index_in() ->
typename std::enable_if<high >= low, unsigned>::type
{
return low == high ? low :
high == low + 1 ? (exists<T, high>() ? high : low) :
exists<T, (high + low)/2>() ? highest_index_in<T, (high+low)/2, high>() :
highest_index_in<T, low, (high+low)/2>();
} // highest_index_in
(where exists
is O(1))
The compilation is extremely slow though (on liveworkspace), and attempting to use wide ranges fail utterly with compiler crashes ([0, ~0u]
does not work...).
I believe I managed to implement the recursion correctly (I would be glad to be contradicted), and yet...
Thus the question: when evaluating the various ternary operators calls here, may the compiler elide the computation of the not-taken branch ?