[Qt-interest] foreach and two template arguments

Thiago Macieira thiago at kde.org
Mon Aug 22 10:21:21 CEST 2011


On Monday, 22 de August de 2011 09:52:25 Schimkowitsch Robert wrote:
> Just out of curiosity (I have bypassed my actual problem using a
> Java-style iterator), is there ANY way I could write the following const
> foreach-loop:

Yes.

> Q_FOREACH(const CGlib_GridData<valueT, deltaT>& gridData, m_GridData)

    typedef CGlib_GridData<valueT, deltaT> Item;
    Q_FOREACH(const Item &gridDate, m_gridData)

> -) I did not manage to create a typedef, because I do not yet have
> concrete types I could set as template arguments

Looks like you do. You pasted them above: valueT and deltaT.

> -) Declaring the loop variable before the Q_FOREACH is not allowed
> -) The preprocessor interprets the commas in the Q_FOREACH macro, so I
> get a syntax error the way it's written right now.

There are two other solutions:

1) #define comma ,

2) forgo Q_FOREACH and use the iterators directly. This produces slightly more 
efficient code.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110822/3a26d805/attachment.bin 


More information about the Qt-interest-old mailing list