2011-01-09 88 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 
     >; 
    }; 
};