diff --git a/include/ircd/util/util.h b/include/ircd/util/util.h index cead68895..71d4200ae 100644 --- a/include/ircd/util/util.h +++ b/include/ircd/util/util.h @@ -163,7 +163,33 @@ at(It &&start, if(!i) return start; - throw std::out_of_range("at(a, b, i): 'i' out of range"); + throw std::out_of_range + { + "at(a, b, i): 'i' out of range" + }; +} + +/// Like std::find() -> std::distance(), but with out_of_range exception +/// +template +typename std::enable_if() || is_input_iterator(), size_t>::type +index(const It &start, + const It &stop, + value &&val) +{ + const auto it + { + std::find(start, stop, std::forward(val)) + }; + + if(likely(it != stop)) + return std::distance(start, it); + + throw std::out_of_range + { + "index(a, b, val): 'val' not contained in set" + }; } //