多变量模板化委托系统 [英] Multiple variadic templated delegate system

查看:133
本文介绍了多变量模板化委托系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我最近使用c ++ 11的可变参数模板构建了一个委托系统,它只是一个魅力。

So, I've recently build a delegate system using c++11's variadic templates, and it works just as a charm.

函数的内置参数在创建时给出(如我想要的),但现在我还想在调用时传递一些(可变)数量的参数。为了实现这一点,我去了,重新编写我的代码,但是我遇到的问题;

However in the system I've built arguments to functions are given at creation-time (as I'd like), but now I'd also like to be able to pass some (variable) number of arguments at call-time. In order to achieve this, I went and redid my code, however is where I'm getting the issues;

error: parameter packs must be at the end of the parameter list
sorry, unimplemented: cannot expand ‘Arguments ...’ into a fixed-length argument list
error: type/value mismatch at argument 1 in template parameter list for ‘template<<declaration error>, class ... Params> class FunctionDelegate’

...还有更多的东西,所以这里有一个代码片段;

... and a lot more to come, so here's a snippet that spawned the issue;

template<typename... Params> 
class Runnable
{
    public:
        virtual void invoke(tuple<Params...> params) = 0;
        virtual ~Runnable() 
        {
        }
};

这是我的两个代理的父类(FunctionDelegate和ObjectDelegate)。这过去是一个未模拟的类(因为invoke没有任何参数),但是因为我有一个可变参数模板列表,我不得不修改我的子类(很明显),我刚刚添加了另一个变量模板;

This is the parent class for my two delegates (FunctionDelegate and ObjectDelegate). This used to be an untemplated class (as invoke didn't take any arguments), however as I've a variadic template list to it, I had to modify my subclasses as well (obviously), and I've just added another variadic template to them;

template<typename... Arguments, typename... Params>
class FunctionDelegate : public Runnable<Params...>
{
    public:
        typedef void (*FunctionType)(Arguments..., Params...);

        FunctionDelegate(FunctionType function, tuple<Arguments...> args)
            : function(function), args(args)
        {
        }

        void invoke(tuple<Params...> params) 
        {
            callFunction(typename gens<sizeof...(Arguments)>::type(), params, typename gens<sizeof...(Params)>::type());
        }
    private:
        template<int... S, int... R>
        void callFunction(seq<S...>, tuple<Params...> params, seq<R...>)
        {
            function(get<S>(args)..., get<R>(params)...);
        }
    private:
        FunctionType function;
        tuple<Arguments...> args;
};

然而,这似乎不允许,至少这是我怀疑的,因此;

This however, does not seem to be allowed, atleast that's what I suspect, so;


  1. 是否允许有两个可变参数模板列表?

  2. 有什么方法可以提示编译器,关于哪个到哪个列表? (即在构造函数中使用的是Arguments,其余的是Params)。

  3. 如果没有办法做到这一点(使用两个可变参数模板列表)可能使用一个,参数和参数在同一可变参数列表,然后简单地使用参数分解到构造函数?

  4. 有没有其他方法来实现我想要的? (除了使用普通的,非类型检查的可变参数传递)。

  1. Is it allowed to have two variadic template lists?
  2. Is there some way, that I can hint the compiler, about which goes to which list? (namely the ones used in the constructor goes to Arguments, and the rest to Params).
  3. If there's simply no way to do this (using two variadic template lists), isn't it possible using one, Arguments and Parameters in the same variadic list, and then simply splitting that using the arguments to the constructor?
  4. Is there any other way to achieve what I'm trying? (except from using the plain old, non type-checked variadic parameter passing).

欢迎任何帮助或见解。

Any help or insight is welcome.

推荐答案

关于您的前两个问题(答案是),C ++ 11标准的第14.1 / 11段规定:

Concerning your first two questions (the answers are "No" and "No"), paragraph 14.1/11 of the C++11 Standard specifies:


如果类的模板参数模板或别名模板具有默认模板参数,则每个后续的
模板参数都应提供默认模板参数,或者是模板参数
pack。 如果主类模板或别名模板的模板参数是模板参数包,则
将是最后一个模板参数
。函数模板的模板参数包不应该是
后跟另一个模板参数,除非该模板参数可以推导出或者具有默认的
参数(14.8.2)。 [示例:

If a template-parameter of a class template or alias template has a default template-argument, each subsequent template-parameter shall either have a default template-argument supplied or be a template parameter pack. If a template-parameter of a primary class template or alias template is a template parameter pack, it shall be the last template-parameter. A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced or has a default argument (14.8.2). [ Example:



template<class T1 = int, class T2> class B; // error
// U cannot be deduced or specified
template<class... T, class... U> void f() { }
template<class... T, class U> void g() { }




-end example]

—end example ]

您的示例显然违反了粗体句子,从而禁止在(主要)类模板上包含多个模板参数包。

Your example clearly violates the sentence in bold, which prohibits having more than one template argument pack on a (primary) class template.

这篇关于多变量模板化委托系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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