mardi 29 novembre 2016

Template specialization for T and function-object that returns T

Is there any way to template a function so that it can accept either a T in the generic case, or to a specialization if the template argument resolves to something that is callable, such as a functor, function pointer or std::function?
For example, I would want something like this:

template<typename T>
void use_this(T obj) {
  obj->do_stuff();
}

template<>
void use_this<???>(??? func) {
  use_this(func());
}

use_this(MyObj); // should call first one
use_this([MyObj](){ return MyObj; }); // should call the second one

Aucun commentaire:

Enregistrer un commentaire