Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Goodbye, Q_FOREACH

A porting guide to C++11 ranged for-loops


Q_FOREACH(int i, container)
    doSomethingWith(i);
Q_FOREACH(const QString &s : functionReturningQStringList())
    doSomethingWith(s);

for (QStringList::const_iterator it = container.begin(), end = container.end(); it != end; ++it)
   doSomethingWith(*it);
for (const auto &s : container)
   doSomethingWith((*it);

Q_FOREACH(const QString &lang, languages)
    languages += getSynonymsFor(lang);

for (const auto &lang : languages)
    languages += getSynonymsFor(lang); // undefined behaviour if
                                       // languages.size() + getSynonymsFor(lang).size() > languages.capacity()

const auto containerCopy = container; // doSomethingWith() may modify 'container' if ....
for (const auto &e : containerCopy)
    doSomethingWith(e);

for (auto end = languages.size(), i = 0; i != end; ++i) // important: cache 'languages.size()'
    languages += getSynonymsFor(languages[i]);

const auto strings = functionReturningQStringList();
for (const QString &s : strings)
    doSomethingWith(s);

for (const QString &s : qAsConst(container))
    doSomethingWith(s);

60 Comments

29 - Aug - 2016

Andy

29 - Aug - 2016

David Johnson

29 - Aug - 2016

Marc Mutz

29 - Aug - 2016

Richard Ash

const auto containerCopy = container;
for (const auto &e : containerCopy) {}
for (auto e : container) {}

29 - Aug - 2016

Marc Mutz

{
  auto && __range = range-init;
  for ( auto __begin = begin-expr,
               __end = end-expr;
            __begin != __end;
            ++__begin ) {
      for-range-declaration = *__begin;
      statement
  }
}

29 - Aug - 2016

Federico

29 - Aug - 2016

Boud

30 - Aug - 2016

Marc Mutz

#ifndef Q_FOREACH
# define Q_FOREACH ... # definition copied from qglobal.h
#endif

30 - Aug - 2016

Boud

30 - Aug - 2016

Marc Mutz

29 - Aug - 2016

Sven Brauch

29 - Aug - 2016

Marc Mutz

29 - Aug - 2016

Sven Brauch

30 - Aug - 2016

Marc Mutz

30 - Aug - 2016

Sven Brauch

30 - Aug - 2016

Marc Mutz

31 - Aug - 2016

Marc Mutz

6 - Sept - 2016

Marc Mutz

30 - Aug - 2016

Kevin Kofler

30 - Aug - 2016

Sven Brauch

29 - Aug - 2016

Kevin Kofler

30 - Aug - 2016

Marc Mutz

24 - Sept - 2017

Mykola Krachkovsky

#define Q_FOREACH(variable, container) \
for (std::pair _container_(true, container); \
    _container_.first; _container_.first = false) \
    for (variable : _container_.second)

24 - Sept - 2017

Mykola Krachkovsky

21 - Sept - 2023

Robert Schimkowitsch

12 - Oct - 2023

Nicolas Arnaud-Cormos

31 - Aug - 2016

Jay Burns

30 - Aug - 2016

Cristian

30 - Aug - 2016

Martin Gräßlin

30 - Aug - 2016

Marc Mutz

30 - Aug - 2016

Martin Gräßlin

30 - Aug - 2016

Marc Mutz

#define QListMdeclare(type)                                                   
class Q_EXPORTM QListM(type) : public QGList                                  
{                                                                             
public:                                                                       
    QListM(type)()                      {}                                    
    QListM(type)( const QListM(type) &l ) : QGList(l) {}                      
   ~QListM(type)()                      { clear(); }                          
    QListM(type) &operator=(const QListM(type) &l)                            
                        { return (QListM(type)&)QGList::operator=(l); }       
    uint  count()   const               { return QGList::count(); }           
    bool  isEmpty() const               { return QGList::count() == 0; }      
    bool  insert( uint i, const type *d){ return QGList::insertAt(i,(GCI)d); }
    void  inSort( const type *d )       { QGList::inSort((GCI)d); }           
    void  append( const type *d )       { QGList::append((GCI)d); }           
    bool  remove( uint i )              { return QGList::removeAt(i); }       
    bool  remove()                      { return QGList::remove((GCI)0); }    
    bool  remove( const type *d )       { return QGList::remove((GCI)d); }    
    bool  removeRef( const type *d )    { return QGList::removeRef((GCI)d); } 
    void  removeNode( QLNode *n )       { QGList::removeNode(n); }            
    bool  removeFirst()                 { return QGList::removeFirst(); }     
    bool  removeLast()                  { return QGList::removeLast(); }      
    type *take( uint i )                { return (type *)QGList::takeAt(i); } 
    type *take()                        { return (type *)QGList::take(); }    
    type *takeNode( QLNode *n )         { return (type *)QGList::takeNode(n);}
    void  clear()                       { QGList::clear(); }                  
    int   find( const type *d )         { return QGList::find((GCI)d); }      
    int   findNext( const type *d )     { return QGList::find((GCI)d,FALSE);} 
    int   findRef( const type *d )      { return QGList::findRef((GCI)d); }   
    int   findNextRef( const type *d ){ return QGList::findRef((GCI)d,FALSE);}
    uint  contains( const type *d ) const { return QGList::contains((GCI)d); }
    uint  containsRef( const type *d ) const                                  
                                        { return QGList::containsRef((GCI)d);}
    type *at( uint i )                  { return (type *)QGList::at(i); }     
    int   at() const                    { return QGList::at(); }              
    type *current()  const              { return (type *)QGList::get(); }     
    QLNode *currentNode()  const        { return QGList::currentNode(); }     
    type *getFirst() const              { return (type *)QGList::cfirst(); }  
    type *getLast()  const              { return (type *)QGList::clast(); }   
    type *first()                       { return (type *)QGList::first(); }   
    type *last()                        { return (type *)QGList::last(); }    
    type *next()                        { return (type *)QGList::next(); }    
    type *prev()                        { return (type *)QGList::prev(); }    
    void  toVector( QGVector *vec )const{ QGList::toVector(vec); }            
private:                                                                      
    void  deleteItem( GCI d ) { if ( del_item ) delete (type *)d; }           
}

31 - Aug - 2016

Kai Koehne

31 - Aug - 2016

Marc Mutz

31 - Aug - 2016

Sven Brauch

31 - Aug - 2016

Marc Mutz

31 - Aug - 2016

Philippe

31 - Aug - 2016

Marc Mutz

31 - Aug - 2016

Philippe

QStringList list;
list << QLatin1String("hello");

for (const QString& s : list)
    qDebug() << s;

for (auto& s : qAsConst(list))
    qDebug() << s;

31 - Aug - 2016

Marc Mutz

31 - Aug - 2016

moo moo moo

31 - Aug - 2016

Marc Mutz

31 - Aug - 2016

Andrius Štikonas

1 - Sept - 2016

Frank Reininghaus

3 - Sept - 2016

Johannes Schaub

3 - Sept - 2016

Marc Mutz

6 - Sept - 2016

Philippe

6 - Sept - 2016

Marc Mutz

6 - Sept - 2016

Philippe

6 - Sept - 2016

Marc Mutz

8 - Sept - 2016

Fred J

8 - Sept - 2016

Philippe

9 - Sept - 2016

Fred J

3 - Oct - 2016

Patrick

23 - Oct - 2016

David Faure

25 - Apr - 2017

Markus

25 - Apr - 2017

Marc Mutz

24 - Aug - 2017

Alexander Volkov

8 - Nov - 2017

mkuhn

QStringList QString::split(const QString &sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
const QStringList QString::split(const QString &sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

8 - Nov - 2017

Marc Mutz

01_NoPhoto

Marc Mutz

Former KDAB employee

Learn Modern C++

Learn more