正向声明boost.type_erasure引用类型

forward-declaring boost.type_erasure reference type

本文关键字:erasure 引用类型 type boost 声明      更新时间:2023-10-16

我在我的代码库中使用boost.type_erasure。到目前为止,考虑到它所能做到的,一直都很好。

我希望能够为我的API转发声明一个"类型擦除引用类型"。这将离开usingtypedef之外,因为它们不允许转发声明。

对于值类型,一切都很好:

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.hp:1290:12:error:在"boost::disable_if,has_update>"中没有名为"type"的类型,boost:"type_erasure":_self>,has_exit,has_get_resolution(),const boost:na,mpl_:na,mpl::na,mpl_::na,mpl_ e_root,boost::type_erasure::copy_structible,boost::type_erasure::related,mpl_::na,mpl_:na,mpl_::na,mpl_

::类型*=0~~~^~~~../libbarvie/src/barvie/controller/any_game_state.hp:95:17:注意:在成员函数'boost::type_erasure::any,has_update>)的实例化中,boost:,type_erasture::_self>,has_exit,has_get_resolution(),const boost:;type_erassure::_self>,has_get-position mpl_:na,mpl_::na,mpl_这里有要求的使用base::base;^src/barvie/controller/game_action_state.cpp:291:18:注意:当将推导出的模板参数替换为函数模板"any_game_state_ref"(概念2=boost::mpl::vector,has_update>)时,boost::type_erasure::_self>,has_exit,has_get_resolution,const boost::type_erasure::_self>,has_get_scene_root,boost:,type_erasure::copy_constructive,boost::type_erasure::relaxed,mpl_:na,mpl_::na,mpl_::nampl_:,mpl_:na,mpl.::na mpl_:(这,^在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.hp:1323:12:error:在"boost::disable_if,has_update>"中没有名为"type"的类型,boost:"type_erasure":_self>,has_exit,has_get_resolution(),const boost:na,mpl_:na,mpl::na,mpl_::na,mpl_ e_root,boost::type_erasure::copy_structible,boost::type_erasure::related,mpl_::na,mpl_:na,mpl_::na,mpl_::类型=0~~~^~~~../libbarvie/src/barvie/controller/any_game_state.hp:95:17:注意:在成员函数'boost::type_erasure::any,has_update>)的实例化中,boost:,type_erasture::_self>,has_exit,has_get_resolution(),const boost:;type_erassure::_self>,has_get-position mpl_:na,mpl_::na,mpl_这里有要求的使用base::base;^src/barvie/controller/game_action_state.cpp:291:18:注意:当将推导出的模板参数替换为函数模板"any_game_state_ref"(概念2=boost::mpl::vector,has_update>)时,boost::type_erasure::_self>,has_exit,has_get_resolution,const boost::type_erasure::_self>,has_get_scene_root,boost:,type_erasure::copy_constructive,boost::type_erasure::relaxed,mpl_:na,mpl_::na,mpl_::nampl_:,mpl_:na,mpl::na amp;](*这个,

我找到了一个解决方案。解决方案是放弃引用类型的继承构造函数,而不是继承构造函数,我使用了一个等效的手动继承构造函数。似乎模板与继承的构造函数混合在一起可能有些地方没有发挥好作用:

template <class...Args>
any_game_state_ref(Args &&... args) : base(std::forward<Args>(args)...)
{}

这就成功了,现在我也可以转发声明类型擦除的引用了。