我在我的代码库中使用boost.type_erasure。到目前为止,这一经验,鉴于它的能力,一直是非常好的。
我希望能够为我的API转发声明一个“类型擦除引用类型”。这不包括using和typedef,因为它们不允许前向声明。
对于一个值类型,一切都很好:
class any_game_state : public
boost::type_erasure::any<
boost::mpl::vector<has_enter<void ()>,
has_update<bool (std::chrono::milliseconds)>,
has_exit<int ()>,
has_get_resolution<std::pair<int, int> (),
const
boost::type_erasure::_self>,
has_get_position<std::pair<int, int> (),
const
boost::type_erasure::_self>,
has_get_scene_root<boost::any (), const
boost::type_erasure::_self>,
boost::type_erasure::copy_constructible<>,
boost::type_erasure::relaxed>> {
using base = boost::type_erasure::any<...identical_contents>;
using base::base;
};但是,当我试图对其相应的参考做同样的工作时,它是行不通的:
class any_game_state_ref : public
boost::type_erasure::any<
boost::mpl::vector<has_enter<void ()>,
has_update<bool (std::chrono::milliseconds)>,
has_exit<int ()>,
has_get_resolution<std::pair<int, int> (),
const
boost::type_erasure::_self>,
has_get_position<std::pair<int, int> (),
const
boost::type_erasure::_self>,
has_get_scene_root<boost::any (), const
boost::type_erasure::_self>,
boost::type_erasure::copy_constructible<>,
boost::type_erasure::relaxed>,
boost::type_erasure::_self&> {
using base = boost::type_erasure::any<...identical_contents>;
using base::base;
};如果我对“引用类型”使用using,一切都可以正常工作,但我失去了转发-声明引用类型的能力。
我得到的错误如下:
在src/barvie/controller/game_action_state.cpp:3:包含的文件中,在/usr/local/include/boost/type_erasure/any_cast.hpp:24: /usr/local/include/boost/type_erasure/any.hpp:1290:12:错误包含的文件中:在'boost::disable_if,has_update >中没有名为' type‘的类型,boost::type_erasure::_self>,has_exit,has_get_resolution (),const boost::type_erasure::_self>,has_get_position (),const boost::type_erasure::_self>,has_get_scene_root,boost::type_erasure::copy_constructible,boost::type_erasure::relaxed,mpl_::na,mpl_::na>,boost::mpl::vector,( has_update >),boost::type_erasure::_self>,has_exit,has_get_resolution (),const boost::type_erasure::_self>,has_get_position (),const boost::type_erasure::_self>,has_get_scene_root,boost::type_erasure::copy_constructible,boost::type_erasure::relaxed,mpl_::na,mpl_::na,mpl_::na,mpl_::na> >,boost::is_const,mpl_::bool_,mpl_::bool_,mpl_::bool_ >,void>‘
*类型*=0 ~^~ ../libbarvie/src/barvie/controller/any_game_state.hpp:95:17:注:在成员函数'boost::type_erasure::any,has_update >,boost::type_erasure::_self>,has_exit,has_get_resolution (),const boost::type_erasure::_self>,has_get_position (),const boost::type_erasure::_self>,has_get_scene_root,boost::type_erasure::copy_constructible,boost::type_erasure::relaxed,mpl_::na,mpl_::na>,boost::type_erasure::_self &>::any‘在这里请求使用碱基::base;^ src/barvie/controller/game_action_state.cpp:291:18:注:在将导出的模板参数替换为函数模板'any_game_state_ref‘时,使用Concept2 = boost::mpl::vector,has_update >、boost::type_erasure::_self>、has_exit、has_get_resolution ()、const boost::type_erasure::_self>、has_get_position ()、const boost::type_erasure::_self>、has_get_scene_root,boost::type_erasure::copy_constructible,boost::type_erasure::relaxed,mpl_::na,mpl_::na>,Tag2 = boost::type_erasure::_self (这个,^在src/barvie/controller/game_action_state.cpp:3:中包含在/usr/local/include/boost/type_erasure/any_cast.hpp:24: /usr/local/include/boost/type_erasure/any.hpp:1323:12:错误中的文件中:在'boost::disable_if,has_update >中没有名为' type‘的类型,boost::type_erasure::_self>,has_exit,has_get_resolution (),boost::type_erasure::_self>,has_get_position (),const boost::type_erasure::_self>,has_get_scene_root,boost::type_erasure::copy_constructible,boost::type_erasure::relaxed,mpl_::na,mpl_::na,mpl_::na>,boost::mpl::vector.has_update >),boost::type_erasure::_self>,has_exit,has_get_resolution (),const boost::type_erasure::_self>,has_get_position (),const boost::type_erasure::_self>,has_get_scene_root,boost::type_erasure::copy_constructible,boost::type_erasure::relaxed,mpl_::na,mpl_::na,mpl_::na,mpl_::na> >,boost::is_const,mpl_::bool_,mpl_::bool_,mpl_::bool_ >类型=0 ~^~ ../libbarvie/src/barvie/controller/any_game_state.hpp:95:17:注:在成员函数'boost::type_erasure::any,has_update >,boost::type_erasure::_self>,has_exit,has_get_resolution (),const boost::type_erasure::_self>,has_get_position (),const boost::type_erasure::_self>,has_get_scene_root,boost::type_erasure::copy_constructible,boost::type_erasure::relaxed,mpl_::na,mpl_::na>,boost::type_erasure::_self &>::any‘在这里请求使用碱基::base;^ src/barvie/controller/game_action_state.cpp:291:18:注:在将导出的模板参数替换为函数模板'any_game_state_ref‘时,使用Concept2 = boost::mpl::vector,has_update >、boost::type_erasure::_self>、has_exit、has_get_resolution ()、const boost::type_erasure::_self>、has_get_position ()、const boost::type_erasure::_self>、has_get_scene_root,boost::type_erasure::copy_constructible,boost::type_erasure::relaxed,mpl_::na,mpl_::na>,Tag2 = boost::type_erasure::_self &(*这个,
发布于 2015-05-08 08:58:12
我找到了解决办法。解决方案是放弃对引用类型的继承构造函数,而不是使用一个等效的手工继承构造函数来代替继承构造函数。似乎有些关于模板与继承构造函数混合在一起的东西发挥得并不好:
template <class...Args>
any_game_state_ref(Args &&... args) : base(std::forward<Args>(args)...)
{}这起到了作用,现在我也可以转发声明类型擦除引用了。
https://stackoverflow.com/questions/30118073
复制相似问题