On 19 August 2016 at 07:29, David Wohlferd wrote:
> Interesting.  Seems slightly strange, but I've seen stranger.  I guess it's
> seen as "cleaner" than forcing this into 2 statements.

That's not the reason for the C++ rule. It's that you don't
necessarily know what the return type is, e.g.

template<typename ReturnType>
ReturnType
wrapper(ReturnType (*funcptr)(int)) {
  return funcptr(99);
}

void f1(int) { }
int f2(int i) { return 2 * i; }

int main() {
  wrapper(f1);
  wrapper(f2);
}

For the first call ReturnType is deduced as void, but the expression
in the return statement has type void, so it's OK.

Without that rule you'd have to write more code to handle the special
case of void:

void wrapper(void (*funcptr)(int) { funcptr(99); }

template<typename ReturnType>
ReturnType
wrapper(ReturnType (*funcptr)(int)) {
  return funcptr(99);
}

Reply via email to