正向声明boost.type_erasure引用类型
forward-declaring boost.type_erasure reference type
我在我的代码库中使用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.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)...)
{}
这就成功了,现在我也可以转发声明类型擦除的引用了。
- 强制转换为引用类型
- 自定义引用类型
- Java 中是否有类似于 C++ 中引用类型"&"的内容?
- 为什么我不能在运算符=中使用引用类型?
- 可变参数模板函数参数和引用类型推导
- 隐式可转换参数,但属于引用类型
- 如何告诉自动推断向量<bool>元素的非引用类型
- 为什么"const auto [x, y]"绑定到引用类型时没有按预期运行?
- 引用类型的数据成员提供有关恒常正确性"loophole"
- 为什么引用类型在使用临时对象访问时是左值
- 什么是常量指针常量引用类型的参数?(const X* const & p)
- 在 lambda 中从引用类型捕获的值的类型,不使用通用捕获
- 如果类在 C++ 中具有常量或引用类型的非静态数据成员,为什么编译器不提供默认赋值运算符?
- void*作为通用引用类型是如何工作的
- 强制转换为不相关的引用类型是否违反严格的别名规则?
- C++ 引用类型作为递归函数参数
- EXPECT_CALL具有 unique_ptr 引用类型参数的模拟函数
- 如果使用返回引用的函数初始化"auto"var,为什么它不声明引用类型?
- 为什么模板引用类型不能用作模板类型别名参数?
- 模板默认参数将丢失其引用类型