有一个通用的方法来适应功能模板是一个多态函数对象吗? [英] Is there a generic way to adapt a function template to be a polymorphic function object?

查看:177
本文介绍了有一个通用的方法来适应功能模板是一个多态函数对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些函数模板,例如

I have some function templates, for example

template <typename T>
void foo(T);

template <typename T>
void bar(T);

// others

,我需要将每一个传递给算法将调用它与各种类型,例如

and I need to pass each one to an algorithm that will call it with various types, e.g.

template <typename F>
void some_algorithm(F f)
{
    // call f with argument of type int
    // call f with argument of type SomeClass
    // etc.
}



我不能传递我的函数模板无法实例化它与任何特定类型,因为 some_algorithm 将需要调用它的几个不同类型的参数。

I can't pass in my function template uninstantiated, but I can't instantiate it with any specific type either because some_algorithm will need to call it with arguments of several different types.

将我的函数模板修改为多态函数对象,例如

I could adapt my function templates to be polymorphic function objects, e.g.

struct foo_polymorphic
{
    template <typename T>
    void operator()(T t)
    {
        foo(t);
    }
};

,然后将其作为 some_algorithm(foo_polymorphic())。但是这需要为每个函数模板编写一个单独的适配器。

and then pass it as some_algorithm(foo_polymorphic()). But this requires writing a separate adapter for each of my function templates.

有一个通用方法来调整函数模板为多态函数对象,即一些机制,我可以重用每个功能模板我需要适应,而不必为每个单独声明一个单独的

Is there a generic way of adapting a function template to be a polymorphic function object, i.e. some mechanism that I can re-use for each of the function templates I need to adapt, without having to declare something separately for each one?

推荐答案

问题的简短版本给出一个重载的名称 f ,如何简洁地写一个对象 ff 使得 ff(a0,a1,a2,...)最终调用 f(a0,a1,a2,... )

The short version of the problem is given an overloaded name f, how to concisely write an object ff such that ff(a0, a1, a2, ...) ultimately calls f(a0, a1, a2, ...).

多态函数,你如何指出自己,是通常的解决方案。但是它必须在行之外定义(因为它有一个模板成员),所以我会认为这不够简洁,我的回答的目的。

A polymorphic functor, how you point out yourself, is the usual solution. But it must be defined out of line (since it has a template member), so I'll consder that not concise enough for the purposes of my answer.

目前lambda

// set of functions overloaded on int and double
void f(int);
void f(double);

auto ff = [](int i) { return f(i); };

正如GMan在评论中指出的,多态性lambdas会(应该是)简洁地写多态

As GMan pointed out in the comments polymorphic lambdas would (should?) be the solution to concisely write polymorphic functors inline.

在此期间,可以编写一个 make_overload 帮助器,它将多个函子组合成一个,这样

In the meantime, it is possible to write a make_overload helper that combines multiple functors into one, such that

auto ff = make_overload(
    [](int arg0) { return f(arg0); }
    , [](double arg0) { return f(arg0); } );

会捕获整个重载集合。也许一个Boost.Preprocessor宏可以帮助这里,所以 auto ff = POLYMORPHIC_LAMBDA(1,(int)(double),{return f(arg0);}); be使用内联。我怀疑有arity限制然而(因此第一个宏参数),不同于通常的out-of-line手写多态函子解决方案;所以这不会帮助。可变函数模板。

would 'capture' the whole overload set. Perhaps a Boost.Preprocessor macro could help here, so that auto ff = POLYMORPHIC_LAMBDA( 1, (int)(double), { return f(arg0); } ); be used inline. I suspect there are arity restrictions however (hence the first macro argument), unlike the usual out-of-line hand-written polymorphic functor solution; so this wouldn't help with e.g. variadic function templates.

这篇关于有一个通用的方法来适应功能模板是一个多态函数对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆