【问题标题】:Q_DECLARE_METATYPE a boost::multi_arrayQ_DECLARE_METATYPE 提升::multi_array
【发布时间】:2015-08-31 15:18:30
【问题描述】:

我正在尝试使用 Qt 的信号和插槽机制传递一个表示为 boost::multi_array 的多维数组。我尝试使用以下代码声明元类型:

Q_DECLARE_METATYPE(boost::multi_array<double, 2>)

但是我收到以下编译错误(在 MSVC 2015 上):

path\to\project\metatypes.h(7): error C2976: 'boost::multi_array': too few template arguments
..\..\ml_project\boost-libs\include\boost/multi_array.hpp(111): note: see declaration of 'boost::multi_array'
path\to\project\metatypes.h(7): error C2332: 'enum': missing tag name
path\to\project\metatypes.h(7): error C2065: 'Defined': undeclared identifier
path\to\project\metatypes.h(7): error C2143: syntax error: missing '>' before ';'
path\to\project\metatypes.h(7): error C2059: syntax error: '>'
path\to\project\metatypes.h(7): error C2976: 'QMetaTypeId': too few template arguments
c:\qt\qt-everywhere-opensource-src-5.5.0\qtbase\include\qtcore\../../src/corelib/kernel/qmetatype.h(1576): note: see declaration of 'QMetaTypeId'
path\to\project\metatypes.h(7): error C2913: explicit specialization; 'QMetaTypeId' is not a specialization of a class template
..\..\ml_project\boost-libs\include\boost/multi_array.hpp(111): note: see declaration of 'boost::multi_array'
..\..\ml_project\boost-libs\include\boost/multi_array.hpp(111): note: see declaration of 'boost::multi_array'
path\to\project\metatypes.h(7): error C2226: syntax error: unexpected type 'quintptr'
path\to\project\metatypes.h(7): error C2143: syntax error: missing ')' before ';'
path\to\project\metatypes.h(7): error C2143: syntax error: missing ';' before '}'

【问题讨论】:

    标签: c++ qt boost


    【解决方案1】:

    double, 2 之间的逗号被解析为宏定义的一部分。解决方法如下:

    选项 #1

    typedef boost::multi_array<double, 2> my_name;
    
    Q_DECLARE_METATYPE( my_name );
    

    选项 #2

    #include <boost/utility/identity_type.hpp>
    
    Q_DECLARE_METATYPE( BOOST_IDENTITY_TYPE( (boost::multi_array<double, 2>) ) );
    

    选项#3

    手写BOOST_IDENTITY_TYPE:

    template <typename T> struct identity_type;
    template <typename T> struct identity_type<void(T)> { typedef T type; };
    
    #define IDENTITY_TYPE(T) typename identity_type<void T>::type
    
    Q_DECLARE_METATYPE( IDENTITY_TYPE( (boost::multi_array<double, 2>) ) );
    

    选项#4

    用预处理器宏替换逗号:

    #define COMMA ,
    Q_DECLARE_METATYPE( boost::multi_array<double COMMA 2> );
    #undef COMMA
    

    【讨论】:

    • 我喜欢身份类型的想法 :)
    猜你喜欢
    • 2018-04-19
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多