提问人:user2987773 提问时间:7/21/2020 最后编辑:user2987773 更新时间:7/22/2020 访问量:181
BOOST_FUSION_ADAPT_STRUCT的替代实现
Alternative implementation for BOOST_FUSION_ADAPT_STRUCT
问:
从链接,BOOST_FUSION_ADAPT_STRUCT用法的用法是
namespace demo
{
struct employee
{
std::string name;
int age;
};
}
BOOST_FUSION_ADAPT_STRUCT(
demo::employee,
(std::string, name)
(int, age))
将用什么实际实现来代替BOOST_FUSION_ADAPT_STRUCT宏的使用。
答:
0赞
sehe
7/22/2020
#1
我可能依赖于编译器/标志。在我的身上:
namespace boost { namespace fusion { namespace traits { template< > struct tag_of<demo::employee > { typedef struct_tag type; }; template< > struct tag_of<demo::employee const> { typedef struct_tag type; }; } namespace extension { template< > struct access::struct_member< demo::employee , 0 > { typedef std::string attribute_type; typedef attribute_type type; template<typename Seq> struct apply { typedef typename add_reference< typename mpl::eval_if< is_const<Seq> , add_const<attribute_type> , mpl::identity<attribute_type> >::type >::type type; constexpr static type call(Seq& seq) { return seq. name; } }; }; template< > struct struct_member_name< demo::employee , 0 > { typedef char const* type; constexpr static type call() { return "name"; } }; template< > struct access::struct_member< demo::employee , 1 > { typedef int attribute_type; typedef attribute_type type; template<typename Seq> struct apply { typedef typename add_reference< typename mpl::eval_if< is_const<Seq> , add_const<attribute_type> , mpl::identity<attribute_type> >::type >::type type; constexpr static type call(Seq& seq) { return seq. age; } }; }; template< > struct struct_member_name< demo::employee , 1 > { typedef char const* type; constexpr static type call() { return "age"; } }; template< > struct struct_size<demo::employee> : mpl::int_<2> {}; template< > struct struct_is_view< demo::employee > : mpl::false_ {}; } } namespace mpl { template<typename> struct sequence_tag; template< > struct sequence_tag<demo::employee> { typedef fusion::fusion_sequence_tag type; }; template< > struct sequence_tag< demo::employee const > { typedef fusion::fusion_sequence_tag type; }; } }
只是为了可读性:
#include <string>
#include <boost/fusion/adapted/struct.hpp>
namespace demo {
struct employee
{
std::string name;
int age;
};
}
namespace boost {
namespace fusion {
namespace traits {
template <> struct tag_of<demo::employee> { typedef struct_tag type; };
template <> struct tag_of<demo::employee const> { typedef struct_tag type; };
} // namespace traits
namespace extension {
template <> struct access::struct_member<demo::employee, 0> {
typedef std::string attribute_type;
typedef attribute_type type;
template <typename Seq> struct apply {
typedef typename add_reference<
typename mpl::eval_if<is_const<Seq>, add_const<attribute_type>,
mpl::identity<attribute_type>>::type>::type
type;
constexpr static type call(Seq& seq) { return seq.name; }
};
};
template <> struct struct_member_name<demo::employee, 0> {
typedef char const* type;
constexpr static type call() { return "name"; }
};
template <> struct access::struct_member<demo::employee, 1> {
typedef int attribute_type;
typedef attribute_type type;
template <typename Seq> struct apply {
typedef typename add_reference<
typename mpl::eval_if<is_const<Seq>, add_const<attribute_type>,
mpl::identity<attribute_type>>::type>::type
type;
constexpr static type call(Seq& seq) { return seq.age; }
};
};
template <> struct struct_member_name<demo::employee, 1> {
typedef char const* type;
constexpr static type call() { return "age"; }
};
template <> struct struct_size<demo::employee> : mpl::int_<2> {};
template <> struct struct_is_view<demo::employee> : mpl::false_ {};
} // namespace extension
} // namespace fusion
namespace mpl {
template <typename> struct sequence_tag;
template <> struct sequence_tag<demo::employee> {
typedef fusion::fusion_sequence_tag type;
};
template <> struct sequence_tag<demo::employee const> {
typedef fusion::fusion_sequence_tag type;
};
} // namespace mpl
} // namespace boost
有关实际替代方案,请参阅
评论