I want to build a function, for example:
template< int ... values> constexpr bool check( int i ) noexcept { switch(i) { case values[0]: case values[1]: ... case values[n-1] :
Can I perform this function?
UPDATE: thanks, now I know how to implement:
template< int ... values> struct checker; template< int head, int ... tail> struct checker<head, tail...> { static constexpr bool apply( int i ) noexcept { return i == head || checker<tail...>::apply(i); } }; template<> struct checker<> { static constexpr bool apply( int ) noexcept { return false; } }; template< int ... values > constexpr bool check(int i) noexcept { return checker<values...>::apply(i); }
UPDATE2: I don't know if this is good or not, but I found this solution:
template<size_t N> constexpr bool any_of( bool( && array)[N], size_t index = 0) noexcept { return (index == N ) ? false : ( array[index] || any_of( std::forward< decltype(array)>(array), 1+index) ); } template< int ... values > constexpr bool check(int i) noexcept { using list = bool[sizeof...(values)]; return any_of( list{ ( i == values) ... } ); } template<> constexpr bool check <>(int i) noexcept { return false; }
c ++ c ++ 11 variadic-templates
Khurshid Normuradov
source share