Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt foreach with templates with multiple parameters

Tags:

c++

qt

I came across a problem with a qt foreach and a template with multiple template parameters.

QVector<Node<T, U> > nodes;
...
[append some data]
...
foreach(const Node<T, U>& node, nodes) {
  ...
}

I'm getting this error:

error: use of undeclared identifier 'Q_FOREACH'

I guess that it is due to the , in the template because the Qt macro does not detect that it inside another template declaration. How can I fix that without using normal for loops or C++11?

like image 904
dominik Avatar asked Jan 08 '13 13:01

dominik


1 Answers

If your compiler supports C++11, you could use

foreach(auto node, nodes) { ... }

or even

for(auto node: nodes) { ... }

Otherwise, you can force the preprocessor to ignore the comma in the template like this:

#define COMMA ,
foreach(const Node<T COMMA U>& node, nodes) { ... }

Or you can use a typedef

typedef Node<T, U> NodeTU;
foreach(const NodeTU& node, nodes) { ... }
like image 129
Bart van Ingen Schenau Avatar answered Oct 28 '22 08:10

Bart van Ingen Schenau