sashoalm
sashoalm

Reputation: 79537

Is the second term in Qt's foreach evaluated only once?

If I have this code:

foreach (QListWidgetItem *ii, selectedItems()) {
    urls.push_back(ii->data(Qt::ToolTip).toString());
}

Would selectedItems() be called only once?

Upvotes: 3

Views: 2016

Answers (1)

Macke
Macke

Reputation: 25690

Yup. It will create a copy of the returned container, and use that. (See Qt foreach keyword documentation)

Related:

  • Since Qt foreach always copies the container, it's best used with either QT-containers (which are copy-on-write) or small STL containers.
  • Boost's foreach handles this better, and avoids copies unless necessary.

Upvotes: 3

Related Questions