I implemented the template function below using variadic, but I am having difficulties in making it more generic. I am using MS VS C++ 2017.
This function essentially checks if an integer value is one of the values provided int templates argument. In theory should be equivalent to a list of logical OR.
template<int TFirst, int...TArgs>
constexpr bool foo(int&& a)
{
if constexpr (sizeof...(TArgs) > 0)
return a == TFirst || foo<TArgs...>(std::forward<int>(a));
return a == TFirst;
}
int iii = 3;
assert(foo<1, 2, 3>(std::forward<int>(iii)); // ok!
I would like to make this function even more generic using other numeric types like double or class enums or even objects.
I tried the code below. It builds with integers, but NOT with doubles.
template<typename T>
struct check
{
template<T TFirst, T...TArgs>
static constexpr bool foo(T&& a)
{
if constexpr (sizeof...(TArgs) > 0)
return a == TFirst || foo<TArgs...>(std::forward<T>(a));
return a == TFirst;
}
};
// test
int iii = 3;
double ddd = 4.0;
check<int>::foo<1, 2, 3>(std::forward<int>(iii)); // ok
check<double>::foo<1.0, 2.0, 3.0>(std::forward < double >(ddd )); // non ok
Error I have with double is
error C2993: 'T': illegal type for non-type template parameter 'TFirst'
error C2672: 'check<double>::foo': no matching overloaded function found
Is there any fix this or better way to make my function more generic?