Q_FOREACH no longer works as intended
-
For some reason Q_FOREACH will only enter the loop once. It has previously (a few months ago) not been a problem with the same code and the same setup.
This is an issue when I use Linux with:
- gcc
- Qt 5.1.1
However not when I use Windows with:
- msvc2012
- Qt 5.1.1
An example of a Q_FOREACH with this exact behaviour:
QMap<QString, QString> map; map["KeyOne"] = QStringLiteral("ValueOne"); map["KeyTwo"] = QStringLiteral("ValueTwo"); Q_FOREACH(QString key, map.keys()) { // Will only enter once, with KeyOne }
-
For some reason Q_FOREACH will only enter the loop once. It has previously (a few months ago) not been a problem with the same code and the same setup.
This is an issue when I use Linux with:
- gcc
- Qt 5.1.1
However not when I use Windows with:
- msvc2012
- Qt 5.1.1
An example of a Q_FOREACH with this exact behaviour:
QMap<QString, QString> map; map["KeyOne"] = QStringLiteral("ValueOne"); map["KeyTwo"] = QStringLiteral("ValueTwo"); Q_FOREACH(QString key, map.keys()) { // Will only enter once, with KeyOne }
@jamr
I don't know the answer to why you say you see a change in behaviour (differentgcc
version??). You might like to read https://stackoverflow.com/questions/10522155/how-does-q-foreach-foreach-macro-work-and-why-is-it-that-complex for detailed discussion as to what it does undergcc
vsmsvc
. As described in, say, https://www.kdab.com/goodbye-q_foreach/,Q_FOREACH
has become deprecated. Maybe it's time to consider moving on as suggested there and elsewhere? -
Yes, rewriting with regular for-loops would be an option. C++03 is used so I cant really use most of the suggestions there.