变体模板 [英] Variadic templates

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

问题描述

C ++ 0x将允许模板获取任意数量的参数。

C++0x will allow template to take an arbitrary number of arguments. What is the best use of this feature other than implementing tuples ?

推荐答案


  1. Type-安全printf

  2. 工厂方法中转发任意多个构造函数参数

  3. / strong>基本类允许放置和删除有用的策略

  4. 通过将异类类型对象<

  5. 有一个文字运算符可以计算用户定义文字的值(例如10110b)。

  1. Type-safe printf
  2. Forwarding of arbitrary many constructor arguments in factory methods
  3. Having arbitrary base-classes allows for putting and removing useful policies.
  4. Initializing by moving heterogenous typed objects directly into a container by having a variadic template'd constructor.
  5. Having a literal operator that can calculate a value for a user defined literal (like "10110b").

范例3:

template<typename... T> struct flexible : T... { flexible(): T()... { } };

取样到4:

struct my_container { template<typename... T> my_container(T&&... t) { } };
my_container c = { a, b, c };

取样到5:

template<char... digits>
int operator "" b() { return convert<digits...>::value; }

查看此示例代码:这里

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

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