2011-01-09 58 views

回答

11

我已经使用Boost.Mpl生成变体类。

例如,给定一个MPL类型列表,诸如这样的:

typedef boost::mpl::set<Foo, Bar, Baz> type_set; 

然后我使用boost::mpl::fold建立从每个其他派生类链,其中的每个类型添加类型之一的std::unordered_set组。最终结果是一个包含unordered_set<Foo>unordered_set<Bar>unordered_set<Baz>的类。

而且由于在boost::mpl::set方面被指定的类,我可以遍历这些类型的自动生成其它功能,诸如operator==其比较所有unordered_set S的。

4

我使用了一个更强化的尺寸分析库Boost.Units。

我已经开发了编译时反射库中,然后使用该库建立一个通用的类,它提供运行时反射到任何编译时反射型中,我使用。通过支持自动生成的UI组件编辑这些反射类型的属性。

这对我们的应用程序中事件的分布也非常重要。例如,当某人更改他们希望系统所在的单位时,我不必教导该系统已将新项目添加到给定设备,因为代码使用MPL来分析这些类型,并且知道已添加了某些内容并改变它。

我只是用元编程技术Qt的信号包到的东西,恢复的类型安全通过他们的系统中删除,并且能够与任何功能实体连接。

不过说实话,你几乎可以肯定,实际使用已经应用元编程技术时所使用的标准算法,如排序。排序算法的一个体面的实现使用一种较少进化的元编程形式来分析传入的迭代器,然后使用标签分派来启动能够充分利用这些迭代器的特征的排序算法。

坦率地说,如果你不这样做的元编程,然后你不使用C++的力量,你也可以被使用别的东西。

+2

我认为这个问题是关于Boost.MPL,而不是一般的元编程。 – jalf 2011-01-09 13:33:05

+0

如果没有所有导致它的东西,你都不能谈论MPL。 – 2011-01-09 22:30:06

+2

但你可以问“你使用Boost.MPL”而不问“你使用任何其他模板元编程的例子”,就像你可以问“你驾驶沃尔沃”一样,而不问“你开车吗? “ – jalf 2011-04-26 09:58:34

13

事实是,Boost.MPL,像Boost.Preprocessor,真正基石。

大部分的时间,你可能使用通过其他图书馆,一些Boost库都在这两个建造。

例如:

  • Boost.Fusion(横跨编译时和运行时领域之间的间隙)
  • Boost.MultiIndex的(对于一个更简单的接口)
  • 升压。(对于三维分析)
  • Boost.Variant可能,我想,也要看它

你可以用它unknowningly已经:)

3

我在我的stat_log库中广泛使用boost :: mpl(和boost :: fusion)。该库允许用户指定统计和日志记录标签及其相关行为的层次结构,即每个标签的统计类型(直方图,计数器等)。

我在很大程度上依赖于元编程做正确的事与用户所做的:

stat_log::writeStat<IP_PKTS_RCVD>(450); 

例如,如果用户定义类型特点:

template <> 
struct stat_tag_to_type<IP_PKTS_RCVD> 
{ 
    using type = Accumulator< 
     stat_log::HistogramCount< 
      int, 
      1, //start bin 
      1500, //stop bin 
      10 //num_bits 
     > 
    >; 
}; 

的“writeStat”拨打上面会代理(在编译时)到直方图统计量。这种设计技术的强大之处在于“writeStat”调用站点与所选的特定统计信息完全无关。

我还使用了丰富的MPL和boost :: fusion来实际查看统计信息。根据您的问题,请参见以下文件的boost :: MPL的最高浓度:

https://github.com/rjmccabe3701/stat_log/blob/master/include/stat_log/util/stat_log_impl.h https://github.com/rjmccabe3701/stat_log/blob/master/include/stat_log/util/tag_commander.h https://github.com/rjmccabe3701/stat_log/blob/master/include/stat_log/stat_log.h

尤其是漂亮的模板元 “功能” 在stat_log_impl.h:

//This template is used in conjunction with an MPL algorithm 
// with the same semantics as mpl::find_if. 
//BoolFunc is the "condition" metafunction. 
//StatTagFunc is a metafunction that transforms the given 
// stat_tag into something the algorithm requires. 
// For example the "Identity" metafunction would work here. 
//StatTagArgs is extra arguments to the BoolFunc 
template <template<typename...> class BoolFunc, 
      template<typename...> class StatTagFunc, 
      class... StatTagArgs> 
struct tag_node_query 
{ 
    template<typename TheTagNode> 
    struct apply 
    { 
     using stat_tag = typename TheTagNode::tag; 
     using type = std::integral_constant 
     < 
      bool, 
      BoolFunc< 
       typename StatTagFunc<stat_tag>::type, 
       StatTagArgs... 
      >::value 
     >; 
    }; 
};