正向声明boost.type_erasure引用类型

时间:2015-05-08 07:21:15

标签: c++ boost clang

我在代码库中使用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作为&#34;参考类型&#34;一切正常,但我失去了向前声明引用类型的能力。

我得到的错误如下:

在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:错误:没有名为&#39; type&#39; in&#39; boost :: disable_if,has_update&gt;),boost :: type_erasure :: _ self&gt;,has_exit,has_get_resolution(),const boost :: type_erasure :: _ self&gt;,has_get_position(),const boost :: type_erasure: :_self&gt;,has_get_scene_root,boost :: type_erasure :: copy_constructible,boost :: type_erasure :: relaxed,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl_ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na&gt ;, boost :: mpl :: vector,has_update&gt;),boost :: type_erasure :: _ self&gt;,has_exit,has_get_resolution(),const boost :: type_erasure :: _ self&gt;,has_get_position(),const boost :: type_erasure :: _ self&gt;,has_get_scene_root,boost :: type_erasure :: copy_constructible,boost :: type_erasure: :relax,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl_: :na,mpl _ :: na,mpl _ :: na&gt; &gt;,boost :: is_const,mpl _ :: bool_,mpl _ :: bool_,mpl _ :: bool_&gt;,void&gt;&#39;

  

:: type * = 0           ~~~ ^ ~~~   ../libbarvie/src/barvie/controller/any_game_state.hpp:95:17:注意:在成员函数的实例化中&#39; boost :: type_erasure :: any,has_update&gt;),boost :: type_erasure :: _ self&gt ;,has_exit,has_get_resolution(),const boost :: type_erasure :: _ self&gt;,has_get_position(),const boost :: type_erasure :: _ self&gt;,has_get_scene_root,boost :: type_erasure :: copy_constructible,boost :: type_erasure :: relaxed, mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na, mpl _ :: na,mpl _ :: na&gt ;, boost :: type_erasure :: _ self&amp;&gt; :: any&#39;这里要求       使用base :: base;                   ^   src / barvie / controller / game_action_state.cpp:291:18:注意:将推导出的模板参数替换为函数模板&#39; any_game_state_ref&#39; [使用Concept2 = boost :: mpl :: vector,has_update&gt;),boost :: type_erasure :: _ self&gt;,has_exit,has_get_resolution(),const boost :: type_erasure :: _ self&gt;,has_get_position(),const boost :: type_erasure :: _ self&gt;,has_get_scene_root,boost :: type_erasure :: copy_constructible,boost :: type_erasure :: relaxed,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl_ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na&gt;,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:错误:没有名为&#39; type&#39; in&#39; boost :: disable_if,has_update&gt;),boost :: type_erasure :: _ self&gt;,has_exit,has_get_resolution(),const boost :: type_erasure :: _ self&gt;,has_get_position(),const boost :: type_erasure: :_self&gt;,has_get_scene_root,boost :: type_erasure :: copy_constructible,boost :: type_erasure :: relaxed,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl_ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na&gt ;, boost :: mpl :: vector,has_update&gt;),boost :: type_erasure :: _ self&gt;,has_exit,has_get_resolution(),const boost :: type_erasure :: _ self&gt;,has_get_position(),const boost :: type_erasure :: _ self&gt;,has_get_scene_root,boost :: type_erasure :: copy_constructible,boost :: type_erasure: :relax,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl_: :na,mpl _ :: na,mpl _ :: na&gt; &gt;,boost :: is_const,mpl _ :: bool_,mpl _ :: bool_,mpl _ :: bool_&gt;,void&gt;&#39;   :: type = 0           ~~~ ^ ~~~   ../libbarvie/src/barvie/controller/any_game_state.hpp:95:17:注意:在成员函数的实例化中&#39; boost :: type_erasure :: any,has_update&gt;),boost :: type_erasure :: _ self&gt ;,has_exit,has_get_resolution(),const boost :: type_erasure :: _ self&gt;,has_get_position(),const boost :: type_erasure :: _ self&gt;,has_get_scene_root,boost :: type_erasure :: copy_constructible,boost :: type_erasure :: relaxed, mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na, mpl _ :: na,mpl _ :: na&gt ;, boost :: type_erasure :: _ self&amp;&gt; :: any&#39;这里要求       使用base :: base;                   ^   src / barvie / controller / game_action_state.cpp:291:18:注意:将推导出的模板参数替换为函数模板&#39; any_game_state_ref&#39; [使用Concept2 = boost :: mpl :: vector,has_update&gt;),boost :: type_erasure :: _ self&gt;,has_exit,has_get_resolution(),const boost :: type_erasure :: _ self&gt;,has_get_position(),const boost :: type_erasure :: _ self&gt;,has_get_scene_root,boost :: type_erasure :: copy_constructible,boost :: type_erasure :: relaxed,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl_ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na,mpl _ :: na&gt;,Tag2 = boost :: type_erasure :: _ self&amp;]                   (*此,

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。解决方案是放弃引用类型的继承构造函数,而不是继承的构造函数,而是使用等效的手工继承构造函数。看起来关于与继承构造函数混合的模板可能效果不佳:

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

这就是诀窍,现在我也可以转发声明类型擦除的引用。