I am in the middle of writing some generic code for a future library. I came across the following problem inside a template function. Consider the code below:
if you can place the "some generic stuff" in the destructor of a bar
class (inside a security try/catch block, if you're not sure that doesn't throw exceptions, as pointed by Drax), you can simply write
template
auto foo (F &&f)
{
bar b;
return std::forward(f)(/*some args*/);
}
So the compiler compute f(/*some args*/)
, exec the destructor of b
and return the computed value (or nothing).
Observe that return func();
, where func()
is a function returning void
, is perfectly legal.