On SO, I asked for a way to do it without variadics. I also provided a typical "normal" implementation with variadics:

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;

I'd like a review on the above code. Is it safe / OK / missing something, before I make it my standard way of querying type lists?


1 Answer 1


Since you're implementing a traits-like check, I would typically expect your traits class types to be derived from std::true_type and std::false_type. This is certainly not required, and overall this implementation doesn't bug me. I was slightly surprised to see std::tuple<> called out explicitly, but that doesn't seem to change the overall correctness in any way.

The biggest question I have is how exact you want your type matching to be. Do you require exact type matches like std::is_same<T, U> or usably similar types like std::is_convertible<T, U>? To be concrete, what should Is_in_tuple<decltype(0), std::tuple<unsigned int>>::value be? It would be great to have some unit tests that document this sort of edge case.

The only thing I see wrong with your implementation is by implication: either you must have included some form of using, either using namespace std; or using std::tuple;, or you are dependent on someone else having done so before including this header. While correct use of using is fine, particularly in implementation files or inside functions, I would strongly suggest avoiding it here where it will pollute (or require previous pollution of) the compilation unit of whatever code includes this.

  • \$\begingroup\$ This review sent me down a long path trying to figure out how to implement the above traits check without depending on std::tuple. I finally succeeded, and in doing so, I'm tempted to say that the dependency on std::tuple is another potential mistake. But evaluating whether it's a mistake depends on knowing whether your code uses just std::tuple or other custom variadic template classes. \$\endgroup\$ May 19, 2014 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.