210

In the beautiful answer to the copy-and-swap-idiom there is a piece of code I need a bit of help:

class dumb_array
{
public:
    // ...
    friend void swap(dumb_array& first, dumb_array& second) // nothrow
    {
        using std::swap; 
        swap(first.mSize, second.mSize); 
        swap(first.mArray, second.mArray);
    }
    // ...
};

and he adds a note

There are other claims that we should specialize std::swap for our type, provide an in-class swap along-side a free-function swap, etc. But this is all unnecessary: any proper use of swap will be through an unqualified call, and our function will be found through ADL. One function will do.

With friend I am a bit on "unfriendly" terms, I must admit. So, my main questions are:

  • looks like a free function, but its inside the class body?
  • why isn't this swap static? It obviously doesn't use any member variables.
  • "Any proper use of swap will find out swap via ADL"? ADL will search the namespaces, right? But does it also look inside classes? Or is here where friend comes in?

Side-questions:

  • With C++11, should I mark my swaps with noexcept?
  • With C++11 and its range-for, should I place friend iter begin() and friend iter end() the same way inside the class? I think the friend is not needed here, right?
2
  • Considering the side question about range-based for: it's better idea to write member functions and leave the range access on begin() and end() in std namespace (§24.6.5), range-based for internally uses these from global or std namespace (see §6.5.4). However, it comes with downside that these functions are part of <iterator> header, if you do not include it, you might want to write them yourself.
    – Vitus
    Apr 17, 2011 at 20:38
  • 8
    why isn't it static - because a friend function isn't a member function at all.
    – aschepler
    Aug 9, 2016 at 15:29

2 Answers 2

211

There are several ways to write swap, some better than others. Over time, though, it was found a single definition works best. Let's consider how we might think about writing a swap function.


We first see that containers like std::vector<> have a single-argument member function swap, such as:

struct vector
{
    void swap(vector&) { /* swap members */ }
};

Naturally, then, our class should too, right? Well, not really. The standard library has all sorts of unnecessary things, and a member swap is one of them. Why? Let's go on.


What we should do is identify what's canonical, and what our class needs to do to work with it. And the canonical method of swapping is with std::swap. This is why member functions aren't useful: they aren't how we should swap things, in general, and have no bearing on the behavior of std::swap.

Well then, to make std::swap work we should provide (and std::vector<> should have provided) a specialization of std::swap, right?

namespace std
{
    template <> // important! specialization in std is OK, overloading is UB
    void swap(myclass&, myclass&)
    {
        // swap
    }
}

Well that would certainly work in this case, but it has a glaring problem: function specializations cannot be partial. That is, we cannot specialize template classes with this, only particular instantiations:

namespace std
{
    template <typename T>
    void swap<T>(myclass<T>&, myclass<T>&) // error! no partial specialization
    {
        // swap
    }
}

This method works some of the time, but not all of the time. There must be a better way.


There is! We can use a friend function, and find it through ADL:

namespace xyz
{
    struct myclass
    {
        friend void swap(myclass&, myclass&);
    };
}

When we want to swap something, we associate std::swap and then make an unqualified call:

using std::swap; // allow use of std::swap...
swap(x, y); // ...but select overloads, first

// that is, if swap(x, y) finds a better match, via ADL, it
// will use that instead; otherwise it falls back to std::swap

What is a friend function? There is confusion around this area.

Before C++ was standardized, friend functions did something called "friend name injection", where the code behaved as if if the function had been written in the surrounding namespace. For example, these were equivalent pre-standard:

struct foo
{
    friend void bar()
    {
        // baz
    }
};

// turned into, pre-standard:    

struct foo
{
    friend void bar();
};

void bar()
{
    // baz
}

However, when ADL was invented this was removed. The friend function could then only be found via ADL; if you wanted it as a free function, it needed to be declared as so (see this, for example). But lo! There was a problem.

If you just use std::swap(x, y), your overload will never be found, because you've explicitly said "look in std, and nowhere else"! This is why some people suggested writing two functions: one as a function to be found via ADL, and the other to handle explicit std:: qualifications.

But like we saw, this can't work in all cases, and we end up with an ugly mess. Instead, idiomatic swapping went the other route: instead of making it the classes' job to provide std::swap, it's the swappers' job to make sure they don't use qualified swap, like above. And this tends to work pretty well, as long as people know about it. But therein lies the problem: it's unintuitive to need to use an unqualified call!

To make this easier, some libraries like Boost provided the function boost::swap, which just does an unqualified call to swap, with std::swap as an associated namespace. This helps make things succinct again, but it's still a bummer.

Note that there is no change in C++11 to the behavior of std::swap, which I and others mistakenly thought would be the case. If you were bit by this, read here.


In short: the member function is just noise, the specialization is ugly and incomplete, but the friend function is complete and works. And when you swap, either use boost::swap or an unqualified swap with std::swap associated.


†Informally, a name is associated if it will be considered during a function call. For the details, read §3.4.2. In this case, std::swap normally isn't considered; but we can associate it (add it to the set of overloads considered by unqualified swap), allowing it to be found.

27
  • 13
    I disagree that the member function is just noise. A member function allows for e.g. std::vector<std::string>().swap(someVecWithData);, which isn't possible with a swap free function because both arguments are passed by non-const reference.
    – ildjarn
    Apr 18, 2011 at 1:34
  • 3
    @ildjarn: You can do it on two lines. Having the member function violates the DRY principle.
    – GManNickG
    Apr 18, 2011 at 5:18
  • 5
    @GMan : The DRY principle doesn't apply if one is implemented in term of the other. Otherwise no one would advocate a class with implementations of operator=, operator+, and operator+=, but clearly those operators on relevant classes are accepted/expected to exist for symmetry. Same goes for member swap + namespace-scoped swap in my opinion.
    – ildjarn
    Apr 18, 2011 at 5:38
  • 3
    @GMan I think it's considering too much functions. Little known, but even a function<void(A*)> f; if(!f) { } can fail just because A declares an operator! that accepts f equally well as f's own operator! (unlikely, but can happen). If function<>'s author thought "ohh I have an 'operator bool', why should I implement 'operator!'? That would violate DRY!", that would be fatal. You just need to have an operator! implemented for A, and A having a constructor for a function<...>, and things will break, because both candidates will require user defined conversions. Apr 18, 2011 at 8:06
  • 1
    Let's consider how we might think about writing a [member] swap function. Naturally, then, our class should too, right? Well, not really. The standard library has all sorts of unnecessary things, and a member swap is one of them. The linked GotW advocates for member swap function.
    – Xeverous
    Dec 3, 2018 at 14:39
8

That code is equivalent (in almost every way) to:

class dumb_array
{
public:
    // ...
    friend void swap(dumb_array& first, dumb_array& second);
    // ...
};

inline void swap(dumb_array& first, dumb_array& second) // nothrow
{
    using std::swap; 
    swap(first.mSize, second.mSize); 
    swap(first.mArray, second.mArray);
}

A friend function defined inside a class is:

  • placed in the enclosing namespace
  • automatically inline
  • able to refer to static members of the class without further qualification

The exact rules are in section [class.friend] (I quote paragraphs 6 and 7 of the C++0x draft):

A function can be defined in a friend declaration of a class if and only if the class is a non-local class (9.8), the function name is unqualified, and the function has namespace scope.

Such a function is implicitly inline. A friend function defined in a class is in the (lexical) scope of the class in which it is defined. A friend function defined outside the class is not.

14
  • 3
    Actually, friend functions are not placed in the enclosing namespace, in standard C++. The old behavior was called "friend name injection", but was superseded by ADL, replaced in the first standard. See the top of this. (The behavior is quite similar, though.)
    – GManNickG
    Apr 17, 2011 at 18:46
  • 1
    Not really equivalent. The code in the question makes it so that swap is only visible to ADL. It's a member of the enclosing namespace, but its name is not visible to the other name lookup forms. EDIT: I see that @GMan was faster again :) @Ben it's always been that way in the ISO C++ :) Apr 17, 2011 at 18:51
  • 2
    @Ben: No, friend injection never existed in a standard, but it was widely used before which is why the idea (and compiler support) tended to carry on, but it's technically not there. friend functions are only found by ADL, and if they need to just be free functions with friend access, they need to both be declared as friend within the class, and as a normal free function declaration outside the class. You can see that necessity in this answer, for example.
    – GManNickG
    Apr 17, 2011 at 18:53
  • 2
    @towi: Because the friend function is at namespace scope, the answers to all three of your questions should become clear: (1) It is a free function, plus has friend access to private and protected members of the class. (2) It's not a member at all, neither instance nor static. (3) ADL doesn't search within classes but this is ok because the friend function has namespace scope.
    – Ben Voigt
    Apr 17, 2011 at 18:53
  • 1
    @Ben. In the spec, the function is a namespace member, and the phrase "the function has namespace scope" can be interpreted to say that the function is a namespace member (it pretty much depends on the context of such a statement). And it adds a name to that namespace that is only visible to ADL (actually, IIRC some part contradict other parts in the spec on whether or not any name is added. But the addition of a name is needed to detect incompatible declarations added to that namespace, so in fact, an invisible name is added. See the note at 3.3.1p4). Apr 17, 2011 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.