Qt:嵌套foreach上的Wshadow警告

时间:2015-06-08 09:10:27

标签: c++ qt foreach compiler-warnings

我启用了编译器标志" -Wshadow"在Qt项目上。现在我收到项目中每个嵌套的foreach循环的警告。看来,foreach宏使用了一个在嵌套的foreach循环中重复的标识符。

快速举例:

QStringList listA, listB;
foreach (QString a, listA) {
    foreach (QString b, listB) {
        qDebug() << a << b;
    }
}

编译器输出:

Test.cpp:46:9: warning: declaration shadows a local variable [-Wshadow]
        foreach (QString b, listB) {
        ^
/usr/include/qt5/QtCore/qglobal.h:911:21: note: expanded from macro 'foreach'
#    define foreach Q_FOREACH
                    ^
/usr/include/qt5/QtCore/qglobal.h:857:47: note: expanded from macro 'Q_FOREACH'
for (QForeachContainer<__typeof__(container)> _container_(container); \
                                              ^
Test.cpp:45:5: note: previous declaration is here
    foreach (QString a, listA) {
    ^
/usr/include/qt5/QtCore/qglobal.h:911:21: note: expanded from macro 'foreach'
#    define foreach Q_FOREACH
                    ^
/usr/include/qt5/QtCore/qglobal.h:857:47: note: expanded from macro 'Q_FOREACH'
for (QForeachContainer<__typeof__(container)> _container_(container); \
                                              ^

有没有办法启用-Wshadow但是可以抑制来自嵌套foreach的警告?

0 个答案:

没有答案