是否可以“存储”?模板参数包而不扩展它?
当我偶然发现这个问题时,我正在尝试 C++0x 可变参数模板:
template < typename ...Args >
struct identities
{
typedef Args type; //compile error: "parameter packs not expanded with '...'
};
//The following code just shows an example of potential use, but has no relation
//with what I am actually trying to achieve.
template < typename T >
struct convert_in_tuple
{
typedef std::tuple< typename T::type... > type;
};
typedef convert_in_tuple< identities< int, float > >::type int_float_tuple;
当我尝试 typedef 模板参数包时,GCC 4.5.0 给了我一个错误。
基本上,我想将参数包“存储”在 typedef 中,而不解压它。是否可以?如果不允许,是否有某种原因不允许这样做?
I was experimenting with C++0x variadic templates when I stumbled upon this issue:
template < typename ...Args >
struct identities
{
typedef Args type; //compile error: "parameter packs not expanded with '...'
};
//The following code just shows an example of potential use, but has no relation
//with what I am actually trying to achieve.
template < typename T >
struct convert_in_tuple
{
typedef std::tuple< typename T::type... > type;
};
typedef convert_in_tuple< identities< int, float > >::type int_float_tuple;
GCC 4.5.0 gives me an error when I try to typedef the template parameters pack.
Basically, I would like to "store" the parameters pack in a typedef, without unpacking it. Is it possible? If not, is there some reason why this is not allowed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种方法比 Ben 的方法稍微通用一些,如下所示:
Another approach, which is slightly more generic than Ben's, is as follows:
我认为不允许的原因是它会很混乱,但你可以解决它。您需要使用依赖关系反转,并使将参数包存储到工厂模板中的结构能够将该参数包应用于另一个模板。
大致如下:
I think the reason it's not allowed is that it would be messy, and you can work around it. You need to use dependency inversion and make the struct storing the parameter pack into a factory template able to apply that parameter pack to another template.
Something along the lines of:
我发现本·沃伊特的想法对我自己的努力非常有用。我对其进行了稍微修改,使其通用而不仅仅是元组。对于这里的读者来说,这可能是一个明显的修改,但可能值得展示:
名称 TypeWithList 源于这样一个事实:该类型现在是用先前的列表实例化的。
I've found Ben Voigt's idea very useful in my own endeavors. I've modified it slightly to make it general to not just tuples. To the readers here it might be an obvious modification, but it may be worth showing:
The name TypeWithList stems from the fact that the type is now instantiated with a previous list.
这是 GManNickG 巧妙的部分特化技巧的变体。没有委托,并且通过要求使用 variadic_typedef 结构,您可以获得更多的类型安全性。
This is a variation of GManNickG's neat partial specialization trick. No delegation, and you get more type safety by requiring the use of your variadic_typedef struct.