最近,我尝试将boost.type_erasure与前向声明类型一起使用,如下所示:
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/member.hpp>
struct Type;
BOOST_TYPE_ERASURE_MEMBER((HasTest), Test, 1)
using Any = boost::type_erasure::any <
boost::mpl::vector <
HasTest<void(Type&)>,
boost::type_erasure::destructible<>,
boost::type_erasure::relaxed
>
>;
int main() {
Any obj;
}但是,编译器(例如clang)抱怨类型不完整:
/usr/local/include/boost/type_traits/is_base_and_derived.hpp:228:42: error: incomplete type 'Type' used in type trait expression
BOOST_STATIC_CONSTANT(bool, value = (BOOST_IS_BASE_OF(B,D) && ! ::boost::is_same<ncvB,ncvD>::value));最后,我必须将API从引用更改为指针,即用Type*替换Type&。
问题:
是否有可能使上述代码正常工作?
发布于 2015-09-01 13:17:36
您的代码中有一些错误。首先,宏BOOST_TYPE_ERASURE_MEMBER为成员函数接受的参数数取一个参数。所以你想:
BOOST_TYPE_ERASURE_MEMBER((HasTest), Test, 1)接下来,any在Concept上进行模板化,这可以是一个概念列表。你忘了清单的部分,所以你需要:
using Any = any<boost::mpl::vector<
HasTest<void(Type&)>,
relaxed
>>;否则,relaxed没有被包含在Concept中--这允许默认的构造。接下来,您不允许在any中销毁,因此在~any()上得到编译错误。您还需要另外提供destructible<>。
最后,您可以使用前向声明的类型定义Any。它必须通过使用它的类型来完成。以下是汇编:
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/member.hpp>
namespace mpl = boost::mpl;
using namespace boost::type_erasure;
struct Type;
BOOST_TYPE_ERASURE_MEMBER((HasTest), Test, 1)
using Any = any<mpl::vector<
HasTest<void(Type&)>,
relaxed,
destructible<>
>>;
struct Type { };
int main() {
Any obj;
}https://stackoverflow.com/questions/32332105
复制相似问题