Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

In 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 quering type lists?

share|improve this question

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.

share|improve this answer
    
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. –  Michael Urman May 19 at 0:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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