繁体   English   中英

C++11 可变参数 std::function 参数

[英]C++11 variadic std::function parameter

名为test的函数采用std::function<>作为其参数。

template<typename R, typename ...A>
void test(std::function<R(A...)> f)
{
    // ...
}

但是,如果我执行以下操作:

void foo(int n) { /* ... */ }

// ...

test(foo);

Compiler(gcc 4.6.1) 说no matching function for call to test(void (&)(int))

为了使最后一行test(foo)编译并正常工作,我该如何修改test()函数? test()函数中,我需要f类型为std::function<>

我的意思是,是否有任何模板技巧可以让编译器确定函数的签名(例如foo ),并将其自动转换为std::function<void(int)>

编辑

我也想为 lambdas(声明的和无状态的)做这项工作。

看起来你想使用重载

template<typename R, typename ...A>
void test(R f(A...))
{
    test(std::function<R(A...)>(f));
}

这个简单的实现将接受您将尝试传递的大部分功能。 外来函数将被拒绝(如void(int...) )。 更多的工作会给你更多的通用性。

std::function实现了 Callable 接口,即它看起来像一个函数,但这并不意味着您应该要求可调用对象是std::function

template< typename F > // accept any type
void test(F const &f) {
    typedef std::result_of< F( args ) >::type R; // inspect with traits queries
}

鸭子类型是模板元编程中的最佳策略。 接受模板参数时,不要具体,让客户端实现接口。

例如,如果您确实需要std::function来重新定位变量或类似的东西,并且您知道输入是原始函数指针,则可以分解原始函数指针类型并将其重组为std::function

template< typename R, typename ... A >
void test( R (*f)( A ... ) ) {
    std::function< R( A ... ) > internal( f );
}

现在用户无法传递std::function因为它已封装在函数中。 您可以将现有代码保留为另一个重载并仅委托给它,但要注意保持接口简单。

至于有状态的 lambda,我不知道如何处理这种情况。 它们不会分解为函数指针,据我所知,无法查询或推断参数类型。 无论好坏,此信息对于实例化std::function都是必要的。

通常不建议按值接受std::function ,除非您处于“二进制分隔”(例如动态库、“不透明”API),因为正如您刚刚目睹的那样,它们在重载方面造成了严重破坏。 当一个函数确实按值使用std::function ,调用者通常有责任构造对象以避免重载问题(如果函数完全重载)。

然而,由于您编写了一个模板,因此很可能您没有使用std::function (作为参数类型)来获得类型擦除的好处。 如果您想做的是检查任意函子,那么您需要一些特征。 例如 Boost.FunctionTypes 具有诸如result_typeparameter_types 一个最小的功能示例:

#include <functional>

#include <boost/function_types/result_type.hpp>
#include <boost/function_types/parameter_types.hpp>
#include <boost/function_types/function_type.hpp>

template<typename Functor>
void test(Functor functor) // accept arbitrary functor!
{
    namespace ft = boost::function_types;

    typedef typename ft::result_type<Functor>::type result_type;
    typedef ft::parameter_types<Functor> parameter_types;
    typedef typename boost::mpl::push_front<
        parameter_types
        , result_type
    >::type sequence_type;
    // sequence_type is now a Boost.MPL sequence in the style of
    // mpl::vector<int, double, long> if the signature of the
    // analyzed functor were int(double, long)

    // We now build a function type out of the MPL sequence
    typedef typename ft::function_type<sequence_type>::type function_type;

    std::function<function_type> function = std::move(functor);
}

最后一点,我不建议在一般情况下内省函子(即刺激它们的结果类型和参数类型),因为这根本不适用于多态函子。 考虑几个重载的operator() :那么就没有“规范的”结果类型或参数类型。 使用 C++11,最好“热切地”接受任何类型的函子,或者根据需要使用 SFINAE 或static_assert等技术约束它们,然后(当参数可用时)使用std::result_of来检查结果输入一组给定的参数 需要预先约束的情况是当目标是将函子存储到例如std::function<Sig>的容器中时。

为了了解我上一段的意思,用多态函子测试上面的代码片段就足够了。

这是一个旧的,我似乎无法找到关于同一主题的太多内容,所以我想我会继续做一个笔记。

在 GCC 4.8.2 上编译,以下工作:

template<typename R, typename... A>
R test(const std::function<R(A...)>& func)
{
    // ...
}

但是,您不能仅通过传入指针、lambda 等来调用它。但是,以下 2 个示例都可以使用它:

test(std::function<void(int, float, std::string)>(
        [](int i, float f, std::string s)
        {
            std::cout << i << " " << f << " " << s << std::endl;
        }));

还:

void test2(int i, float f, std::string s)
{
    std::cout << i << " " << f << " " << s << std::endl;
}

// In a function somewhere:
test(std::function<void(int, float, std::string)>(&test2));

这些的缺点应该很明显:您必须为它们显式声明 std::function ,这可能看起来有点难看。

尽管如此,我将它与一个可以扩展以调用传入函数的元组放在一起,并且它可以工作,只需要多一点明确说明您正在调用测试函数的内容。

包含元组的示例代码,如果你想玩它: http : //ideone.com/33mqZA

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM