高分求C++ boost库 BOOST_AUTO宏的原理,有满意答案加分你懂得

2025-02-25 11:29:54
推荐回答(1个)
回答1:

主要是利用模板片特化,
模板参数不仅仅可以实现还能是整形数
你看这个
template<>
struct msvc_typeid_wrapper<1> {
typedef msvc_typeid_wrapper<1> type;
};
它实际上就是一个片特化,如果模板参数是1(这个1往往又利用枚举的值来传递),至于变成int,主要是是下面的宏与条件编译联合作用的结果

# define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
struct name {\
BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type(expr)));\
typedef typename boost::type_of::msvc_extract_type::id2type id2type;\
typedef typename id2type::type type;\
};