[Development] QForeachContainer mystery
Joerg Bornemann
joerg.bornemann at digia.com
Mon Feb 10 15:23:30 CET 2014
Consider the following piece of code:
QStringList other;
foreach (QString str, QStringList(other))
; // do something
This builds fine with Visual Studio 2013.
With gcc 4.7.2 and clang 3.0-6.2 we get:
error: request for member 'brk' in '_container_', which is of non-class
type 'QForeachContainer<QStringList>(QStringList)'
To trigger this compile error it's enough to do
QStringList other;
QForeachContainer<__typeof__(QStringList(other))>
_container_(QStringList(other));
_container_.brk = 0;
Interestingly, one can work around this error by wrapping the c'tor with
a function or passing a variable to foreach.
----
QStringList id(QStringList x) { return x; }
...
QStringList other;
foreach (QString str, id(QStringList(other)))
; // do something
----
QStringList other;
QStringList troz(other);
foreach (QString str, troz)
; // do something
----
Looks like __typeof__, which is used when building with a recent gcc,
behaves differently when passing a c'tor?
Could anyone shed light on this mystery?
Cheers,
Joerg
P.S. Yeeees it's quite pointless to use a copy constructor like this in
foreach. The example above is artifical. :)
More information about the Development
mailing list