dimanche 4 septembre 2016

Conditional code generation in template function

I'm trying to write a single template function that will work fine in both of these cases. I have two functions:

template <typename T>
Check(T&)

template <typename U, typename V>
Check(U&, V&)

I have a function which I would like to write a single implementation of, where in case of any other type than a pair, I want it to call Check(T&) and if we have a std::pair, then it should call the two parameter variant of Check. In other words, code like:

void SomeFunction() {
  ...
  if (type(x) is std::pair<U,V>)
    Check(x.first, x.second);
  else
    Check(x);
  ...
}

Is there some way of accomplishing this behavior using template magic? The code should preferably work with C++11. I own the code for the Check functions and they are internal to the class, so I can tweak their interface if necessary.

Aucun commentaire:

Enregistrer un commentaire