vendredi 10 mars 2017

Handling a void variable in a templatized function in C++11

I have a template class that must perform some operation before calling a function whose parameters and return type are generic.

This is the method:

template <typename ReturnType, typename ...Args>
ReturnType function (Args ...args) {
  // prepare for call
  // ...
  ReturnType rv = makeCall(args...);  // [1]
  // dismiss the call
  // ...
  return rv;
}

Of course it's compiling correctly when ReturnType is not void. When I use it in this context:

function<void>(firstArg, secondArg);

The compiler responds with

error: return-statement with a value, in function returning 'void' [-fpermissive]

pointing to the line marked with [1].

Is there any solution other than passing -fpermissive to the compiler? I would prefer to have a unique method, because I possible solution I found is to instantiate different versions using enable_if and is_same.

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire