【发布时间】:2009-12-08 23:30:18
【问题描述】:
阅读一些源代码,我找到了下一个特征定义:
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; \
}; \
}
你们中的任何人都知道为什么第二种方法可能比第一种方法更脆弱,或者更难添加新特征吗?
【问题讨论】:
标签: c++ templates typetraits