QT5.5中qDebug()的新QFlags助手问题

时间:2015-07-17 12:14:59

标签: c++ qt qt5.5

我有以下代码

#include <QDebug>  //Needed for other stuff!!
#include <QDataStream>

class TestClass {
public:
    class SubClass {
    public:
        QString a;
        int b;

        /*
        //Needed for QDataStream but not relevant for the generated error
        friend QDataStream& operator<<(QDataStream& os, SubClass const& f)
        {
            os << f.a;
            os << f.b;
            return os;
        }

        friend QDataStream& operator>>(QDataStream& is, SubClass& f)
        {
            is >> f.a;
            is >> f.b;
            return is;
        }*/

        friend QDataStream& operator<< <SubClass>(QDataStream&, QList<SubClass> const&);
        friend QDataStream& operator>> <SubClass>(QDataStream&, QList<SubClass>&);

    };
};

用qt5.5编译它会给我以下错误:

/usr/include/qt/QtCore/qflags.h:88: error: invalid application of 'sizeof' to an incomplete type 'TestClass::SubClass'
    Q_STATIC_ASSERT_X((sizeof(Enum) <= sizeof(int)),
                   ^~~~~~~~~~~~
/usr/include/qt/QtCore/qtypetraits.h:478: error: 'TestClass::SubClass' is an incomplete type
    : integral_constant<bool, (T(0) < T(-1))> {};
                               ^
/usr/include/qt/QtCore/qdebug.h:279: in instantiation of template class 'QtPrivate::IsQEnumHelper<QFlags<TestClass::SubClass> >' requested here
    QtPrivate::IsQEnumHelper<T>::Value || QtPrivate::IsQEnumHelper<QFlags<T> >::Value,
                                                     ^
main.cpp:25: while substituting explicitly-specified template arguments into function template 'operator<<'
        friend QDataStream& operator<< <SubClass>(QDataStream&, QList<SubClass> const&);
                            ^

原因似乎在于QDebug头文件的以下代码(qdebug.h:277):

template <class T>
inline typename QtPrivate::QEnableIf<
    QtPrivate::IsQEnumHelper<T>::Value || QtPrivate::IsQEnumHelper<QFlags<T> >::Value,
    QDebug>::Type
operator<<(QDebug debug, const QFlags<T> &flags)
{
  //...
}

使用qt5.4进行编译工作正常。采用正确的模板函数(qdatastream.h:241)

template <typename T>
QDataStream& operator<<(QDataStream& s, const QList<T>& l)
{
   //...
}

问题:

  1. 我想知道为什么考虑qdebug.h中的模板。我有一些想法,但是对c ++标准的适当链接以及正确解释的答案会很好。
  2. 是否有解决方法,或者我应该向qt提交错误报告?

1 个答案:

答案 0 :(得分:0)

以下(简化代码)生成相同的错误。

#include <QDebug>  //Needed to cause the error

class TestClassA {};
class TestClassB {};

template <typename T>
TestClassA& operator<<(TestClassA& s, T& l) {
    Q_UNUSED(l)
    return s;
}

template TestClassA& operator<< <TestClassB>(TestClassA& s, TestClassB& l);

Bugreport开场于:https://bugreports.qt.io/browse/QTBUG-47375

<强>更新: 他们修好了。修复将包含在Qt 5.5.1