[Interest] qSwap or std::swap?

Konstantin Tokarev annulen at yandex.ru
Mon Oct 26 16:53:49 CET 2015



26.10.2015, 18:49, "John Weeks" <john at wavemetrics.com>:
>>  Both are correct, since one is implemented in terms of the other. That means
>>  they MUST expand to exactly the same assembly (and they do, I've just tested).
>>
>>  If you're seeing something different, the problem is probably your code. And
>>  that's why Marc is recommending qSwap: because people don't know how to use
>>  std::swap. There's a big gotcha: you MUST NOT write the "std::" part of
>>  std::swap.
>
> At the risk of making a fool of myself, what is the difference? I thought I had learned that namespaces were just a scoping mechanism for names to avoid name collisions. Why does this make such a difference in the generated code?
>
> I'm old enough now that the risk of making a fool of myself is a small price to pay in order to learn something.

Unqualified swap() invokes argument-dependent lookup which may bring in custom overload of swap declared outside of std namespace.

-- 
Regards,
Konstantin



More information about the Interest mailing list