QT5.5 中用于 qDebug() 的新 QFlags 助手的问题
Problems with new QFlags helpers for qDebug() in 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)
{
//...
}
问题:
- 我想知道为什么要考虑 qdebug.h 中的模板。我有一些想法,但最好能有一个适当的 C++ 标准链接和正确解释的答案。
- 是否有解决此问题的方法,或者我应该向 qt 提交错误报告?
以下(简化代码)会产生相同的错误。
#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);
错误报告打开于:https://bugreports.qt.io/browse/QTBUG-47375
更新:
他们修好了。修复将包含在 Qt 5.5.1
我有以下代码
#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)
{
//...
}
问题:
- 我想知道为什么要考虑 qdebug.h 中的模板。我有一些想法,但最好能有一个适当的 C++ 标准链接和正确解释的答案。
- 是否有解决此问题的方法,或者我应该向 qt 提交错误报告?
以下(简化代码)会产生相同的错误。
#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);
错误报告打开于:https://bugreports.qt.io/browse/QTBUG-47375
更新: 他们修好了。修复将包含在 Qt 5.5.1