本文介绍了函数参数返回void或non-void类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为将来的库编写一些通用代码。我在模板函数中遇到以下问题。请考虑以下代码:

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:

template<class F>
auto foo(F &&f) {
    auto result = std::forward<F>(f)(/*some args*/);
    //do some generic stuff
    return result;
}

它将正常工作,除非我将返回的值传递给 void 如:

It will work fine, unless I pass to it a function that returns void like:

foo([](){});

现在,当然,我可以使用一些 std :: enable_if 魔术检查返回类型并为返回 void 的函数执行特化,如下所示:

Now, of course, I could use some std::enable_if magic to check the return type and perform specialization for a function returning void that looks like this:

template<class F, class = /*enable if stuff*/>
void foo(F &&f) {
    std::forward<F>(f)(/*some args*/);
    //do some generic stuff
}

但这会重复很多代码用于实际上逻辑上等效的功能。是否可以轻松地以通用方式轻松完成 void 返回和非 void 返回功能

But that would awfully duplicate code for actually logically equivalent functions. Can this be done easily in a generic way for both void-returning and non-void-returning functions in a elegant way?

编辑:
函数 f()与我想要的通用内容之间存在数据依赖这样做,因此我不会考虑这样的代码:

there is data dependency between function f() and generic stuff I want to do, so I do not take code like this into account:

template<class F>
auto foo(F &&f) {
    //do some generic stuff
    return std::forward<F>(f)(/*some args*/);
}


推荐答案

如果可以将 bar 类的析构函数中的一些常规内容(在安全try / catch块中,如果您不确定是否不会抛出异常,如Drax所指出的) ),您只需编写

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 <typename F>
auto foo (F &&f)
 {
   bar b;

   return std::forward<F>(f)(/*some args*/);
 }

所以编译器计算 f(/ * some args * /),执行 b 的析构函数并返回计算出的值(或不返回任何值)。

So the compiler compute f(/*some args*/), exec the destructor of b and return the computed value (or nothing).

观察该返回func(); ,其中 func()是返回<$ c $的函数c> void 是完全合法的。

Observe that return func();, where func() is a function returning void, is perfectly legal.

这篇关于函数参数返回void或non-void类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:16