mardi 17 mars 2020

How can I determine the return type of a functor which takes that type for a parameter?

Suppose I have to following function:

template <typename Op, typename T> foo(Op op) { }

Op is assumed to have the following method:

T Op::operator()(T&);

(this is a simple case; actually it might have more parameters but their types are known to me).

Now, I want to set a default for the T parameter. The problem is, in order to faux-invoke it (e.g. as in std::result_of<Op::operator()(int&)> I need to have the type of the parameter - which is exactly the type I'm missing.

Is it possible to determine T from Op? So that I may, for example, call:

foo( [](int& x){ return x++; } );

I'm interested in a C++11 solution; if you need a later-standard-version capability, that's also interesting (especially the explanation of why that is).

Aucun commentaire:

Enregistrer un commentaire