本文介绍了模板类的多个变量参数包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我使用可变参数包来进行基于策略的类设计。

I am using variadic parameter packs for policy based class design.

template <APITypes APIType, class... Policies>
class IShader : public Policies... {

};

策略在调用时定义,如果未指定则使用默认值。问题出现在我需要添加另一个变量参数包时:

Policies are defined when called or with defaults if none are specified. The problem comes when I need to add another variadic parameter pack:

template <AttributeType... Attributes, APITypes APIType, class... Policies>
class IShader : public Policies... {

};

这会导致错误模板参数包必须是最后一个模板参数。我计划使用属性包更改至少一个策略的行为。但我无法弄清楚如何在一个模板类中获取两个可变参数包。

This results in the error "Template parameter pack must be the last template parameter". I am planning to use the attribute pack to change the behaviour of at least one of the policies. But I can't work out how to get two variadic parameter packs in one template class.

推荐答案

或者属性列表的某种包装器。

In the discussion comments you expressed a willingness to consider some kind of indirection, or "a wrapper of some kind for the attribute list".

轻量级 std :: tuple 的包装器与专业化可能在这里工作:

A lightweight std::tuple-based wrapper, together with specialization, might work here:

template <typename attribute_tuple, APITypes APIType,
          typename policy_tuple> class IShader;

template <AttributeType... Attributes, APITypes APIType,
          class... Policies>
class IShader<std::tuple<Attributes...>, APIType,
              std::tuple<Policies...>> : public Policies... {

// ...

};

这里的目标是使用以下行的模板实例:

The goal here is to use a template instance along the lines of:

IShared<std::tuple<Attribute1, Attribute2>, APITypeFoo,
        std::tuple<Policy1, Policy2>> ishared_instance;

然后交叉你的手指,这将是专门的模板声明,可供模板专业化单独使用。

And cross your fingers that this is going to match the specialized template declaration, at which point both parameter packs are available for the template specialization to use, individually.

这篇关于模板类的多个变量参数包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:47