Varadiac宏,用于对类进行不同的部分专业化处理 [英] Varadiac macros for making different partial specialization of a class
问题描述
对于某些类,我们可以定义进行显式模板专门化的宏,作为Boost序列化库中的以下示例:
For some classes we can define macros which does explicit template specialization as the folllowing example from Boost Serialization library:
#define BOOST_IS_BITWISE_SERIALIZABLE(T) \
namespace boost { \
namespace serialization { \
template<> \
struct is_bitwise_serializable< T > : mpl::true_ {}; \
}} \
/**/
这适用于像BOOST_IS_BITWISE_SERIALIZABLE(MyClass<int>)
但是我想创建一个便捷宏,该宏可用于部分专业化,其参数如下:
But I would like to create a convenience macro that works for partial specialization with different arguments as following:
template<class T, class Enable>
struct is_bitwise_serializable< MyClassA<T, Enable> > : mpl::true_ {};
template<class T>
struct is_bitwise_serializable< MyClassB<T> > : mpl::true_ {};
template<int N>
struct is_bitwise_serializable< MyClassC<N> > : mpl::true_ {};
.....
我正试图浏览Boost PreProcessor文档以解决此问题,但不能做很多事情.是否为此提供了Boost PreProcessor解决方案?
I was trying to go through Boost PreProcessor documentation for this problem, but could not proceed a lot. Is it there a Boost PreProcessor solution for this?
推荐答案
以下是使用 sequences一起工作的基础上 .
Here is a solution which uses Boost.Preprocessor. It is built on the work with sequences.
#include <boost/mpl/bool.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/arithmetic/sub.hpp>
#include <boost/preprocessor/seq/enum.hpp>
#include <boost/preprocessor/seq/transform.hpp>
#include <boost/preprocessor/seq/size.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#define PARAM_NAME param
#define PARAM(Index) BOOST_PP_CAT(PARAM_NAME, Index)
#define PARAM_DESCRIPTION(Index, Data, ParamType) \
ParamType PARAM(BOOST_PP_SUB(Index, 2))
#define IS_BITWISE_SERIALIZABLE(TemplateClass, Params) \
template \
< \
BOOST_PP_SEQ_ENUM(BOOST_PP_SEQ_TRANSFORM(PARAM_DESCRIPTION,, Params)) \
> \
struct is_bitwise_serializable \
< \
TemplateClass \
< \
BOOST_PP_ENUM_PARAMS(BOOST_PP_SEQ_SIZE(Params), PARAM_NAME) \
> \
> \
: boost::mpl::true_ {};
使用示例:
template <class T, class Enable>
struct MyClassA{};
template <class T>
struct MyClassB{};
template <int N>
struct MyClassC{};
template <class T, template <class> class Base = MyClassB>
struct MyClassD : public Base<T>{};
IS_BITWISE_SERIALIZABLE(MyClassA, (class)(class))
IS_BITWISE_SERIALIZABLE(MyClassB, (class))
IS_BITWISE_SERIALIZABLE(MyClassC, (int))
IS_BITWISE_SERIALIZABLE(MyClassD, (class)(template <class> class))
请参见在线示例.
这篇关于Varadiac宏,用于对类进行不同的部分专业化处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!