讀一些源代碼,我發現下一個特徵定義:類型特徵定義。性狀斑點和元函數
namespace dds {
template <typename Topic> struct topic_type_support { };
template <typename Topic> struct topic_data_writer { };
template <typename Topic> struct topic_data_reader { };
template <typename Topic> struct topic_data_seq { };
}
#define REGISTER_TOPIC_TRAITS(TOPIC) \
namespace dds { \
template<> struct topic_type_support<TOPIC> { \
typedef TOPIC##TypeSupport type; }; \
template<> struct topic_data_writer<TOPIC> { \
typedef TOPIC##DataWriter type; }; \
template<> struct topic_data_reader<TOPIC> { \
typedef TOPIC##DataReader type; }; \
template<> struct topic_data_seq<TOPIC> { \
typedef TOPIC##Seq type; }; \
}
這看起來怪我。我會分組所有性狀獨特的一類這樣的:
namespace dds {
template <typename Topic> struct topic_traits { };
}
#define REGISTER_TOPIC_TRAITS(TOPIC) \
namespace dds { \
template<> struct topic_traits<TOPIC> { \
typedef TOPIC##TypeSupport type_support; \
typedef TOPIC##DataWriter data_writter; \
typedef TOPIC##DataReader data_reader; \
typedef TOPIC##Seq seq_type; \
}; \
}
任何你能弄清楚爲什麼第二種方法可能會比第一個或顯著很難增加新的特徵更加脆弱?
欲瞭解更多元編程谷歌助推MPL – KitsuneYMG 2009-12-09 04:31:51
@Samuel:特質blob。那是我正在尋找的名字!謝謝。我已經訂購了亞伯拉罕書。 – 2009-12-12 11:03:21