Function argument returning void or non-void type

后端 未结 4 1043
心在旅途
心在旅途 2021-01-17 10:37

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:



        
4条回答
  •  悲&欢浪女
    2021-01-17 11:30

    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.

提交回复
热议问题