Actual code : Q_INLINE_TEMPLATE QSet &QSet::subtract(const QSet &other) { QSet copy1(*this); QSet copy2(other); typename QSet::const_iterator i = copy1.constEnd(); while (i != copy1.constBegin()) { --i; if (copy2.contains(*i)) remove(*i); } return *this; } I propose a solution without copy : Q_INLINE_TEMPLATE QSet &QSet::subtract(const QSet &other) { if(&other == this) { this.clear(); } else { for(QSet::iterator it = other.begin(); it != other.end(); ++it) { this.remove(*it); } } return *this; }