mercredi 4 janvier 2017

How to call a function, passing the return value (possibly void) of a functor?

Given a function that calls a templated function argument and calls another function that does something with the returned value:

template <typename T>
void doSomething(T &&) {
    // ...
}

template <typename T_Func>
void call(T_Func &&func) {
    doSomething(func());
}

How can this be extended to work with a functor that returns void? Ideally, I would like to add an overloaded void doSomething() { ... } that is called if func's return type is void.

Currently this just results in an error: invalid use of void expression if func returns void.

Working example on Ideone

Aucun commentaire:

Enregistrer un commentaire