Test whether a type V is among the types of a tuple<...> without variardics
c++, c++11, tuples, visual-studio-2012
Solution
Unfortunately, I can't test it on MSVC2012 right now.
#include <type_traits>
#include <tuple>
template<class Needle, class Haystack, int N = std::tuple_size<Haystack>::value>
struct is_any_of
: std::integral_constant
<
bool,
(std::is_same<Needle, typename std::tuple_element<N-1, Haystack>::type>
::value
|| is_any_of<Needle, Haystack, N-1>::value)
>
{};
template<class Needle, class Haystack>
struct is_any_of<Needle, Haystack, 0>
: std::false_type
{};
#include <iostream>
int main()
{
typedef std::tuple<int, int, char, int, int> t0;
typedef std::tuple<int, int, int, int, int> t1;
std::cout << std::boolalpha << is_any_of<char, t0>::value << "\n";
std::cout << std::boolalpha << is_any_of<char, t1>::value << "\n";
}
Problem
A typical implementation would be like so : ``` template <typename V, typename T> struct Is_in_tuple; template <typename V, typename T0, typename... T> struct Is_in_tuple <V, tuple<T0, T...> > { static const bool value = Is_in_tuple<V, tuple<T...> >::value; }; template <typename V, typename... T> struct Is_in_tuple <V, tuple<V, T...> > { static const bool value = true; }; template <typename V> struct Is_in_tuple <V, tuple<> > { static const bool value = false; }; ``` The problem arises in VS2012 where tuples exist, but variadic templates do not! Is there a workaround, a way to perform such tests without variadic templates ?