函数签名作为模板参数 [英] Function signature as template parameter

查看:164
本文介绍了函数签名作为模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以实现这样的功能:

template<typename Signature>
class Test
{
    public:
        //here I want operator () to respect the signature
};

Test<void(int)>          t1; //void operator()(int)
Test<void(int, float)>   t2; //void operator()(int, float)

返回类型总是 void

我想作为模板参数发送函数签名。这可能吗?
我不能使用可变参数模板,因为我的编译器不支持这个功能。

I want to send as template parameter the function signature. Is this possible? I can't use variadic templates as my compiler doesn't support this feature yet.

推荐答案

template <class Ty>
class Test; /* not defined */
template <class Ret, class Arg0>
class Test<Ret(Arg0)> { /* whatever */ }
template <class Ret, class Arg0, class Arg1>
class Test<Ret(Arg0, Arg1)> { /* whatever */ }
template <class Ret, class Arg0, class Arg1, class Arg2>
class Test<Ret(Arg0, Arg1, Arg2)> { /* whatever */ }

继续冗长的重复,直到有足够的参数满足您的需求。在TR1中,建议各种函数对象模板能够处理10个参数。这通常用相当复杂的宏来实现,以简化编码,但它可以通过暴力进行。

Continue the tedious repetition until you have enough arguments for your needs. In TR1 it was recommended that the various function object templates be able to handle 10 arguments. This was typically implemented with fairly sophisticated macros to simplify coding, but it can be done by brute force.

这篇关于函数签名作为模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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