7

I've read that when you're swaping things in c++, you should always using std::swap;, then call swap unqualified, so it automatically picks the std:: ones for std:: and builtin types, your custom one for custom types, and the templated std:: one for everything else.

So, can I just put using std::swap; in the header that every file includes and not have to worry about it?

I understand that avoiding using in a header is common practice. However, is there a problem with it in this particular case?

Dan
  • 12,409
  • 3
  • 50
  • 87
  • possible duplicate of [using declaration in header files](http://stackoverflow.com/questions/22201937/using-declaration-in-header-files) – aruisdante Feb 25 '15 at 21:25
  • 4
    @aruisdante No, it isn't. OP is asking about one particular usage. – Pradhan Feb 25 '15 at 21:30
  • @Pradhan one particular usage does not make polluting the global namespace with `using` any more valid, I would think. – aruisdante Feb 25 '15 at 21:32
  • @aruisdante It *might*, if it is true that calling `swap` without introducing `std::swap` into the current scope is almost always an error. That is what OP is effectively asking. So that makes the other question not a duplicate. Please note that I am not expressing an opinion about the practice - just pointing out that there might be a more a slightly more nuanced answer to this question. – Pradhan Feb 25 '15 at 21:36
  • You can use it in a header file, as long as you use it inside a function, in a header file, so that its scope is limited to that function. This is a common practice. You should *not* put it at file scope in a header file. – Mark Feb 25 '15 at 23:12
  • I wonder if it will work to create your own superswap function which wraps this for you, and use superswap in all your code instead. – Neil Kirk Feb 26 '15 at 01:20

2 Answers2

7

The guidance for swap is to using std::swap at the most local scope possible. For certain, one in a header file that's widely included does not meet this requirement. It still pollutes the global namespace in unexpected ways (someone not expecting std::swap will be imported to the global namespace) and should be avoided just like using namespace.

Mark B
  • 95,107
  • 10
  • 109
  • 188
0

The principle issue here is that you are assuming that people did not write their own swaps that happen to be better matches that do subtly or completely different things to the semantics of std::swap and related friends. For a simple example, consider

void swap(int* a, int* b);

where the contents of the pointers are swapped. Now try to swap a pair of int*. You might think that the pointers are swapped, but instead, surprise! it's the contents that are getting swapped.

Really, the guidance for swap is the same as any other- only using at the most local scope possible and no earlier and definitely not in a header.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • If someone did write `void swap(int *a, int *b);` and your function did `using std::swap; swap(x, y);` on two pointers to int, then you'd get the same problem (it finds the better match instead of using `std::swap`). So I think the issue here is that the bogus swap function messes with everything. – M.M Feb 26 '15 at 00:52
  • @MattMcNabb No, unqualified lookup stops as soon as the name is found in the local scope; you won't pickup the bogus swap declared outside. And there's no ADL, either. – T.C. Feb 26 '15 at 05:26