Implementing member swap() in terms of non-member swap()

std::swap
std::swap pointers
swap is not a member of std
std::move
c++ swap function implementation
std::swap implementation
swap() in c
c++ swap vector elements

I'm implementing a class with a similar interface to std::array, which has both the member swap() and the non-member swap().

Since I want my class to mimic the standard containers, I would like to implement both kinds of swap() (the non-member swap() is implemented via ADL, since specializing std::swap() isn't permitted):

class A {
    public:
        friend void swap(A& a, A& b) { /* swap the stuff */ }
        void swap(A& other) { swap(*this, other); }
};

However, it seems like I can't call the non-member swap() from inside the class, because it prefers the member swap() even though it only has a single parameter. Changing it to ::swap(*this, other) doesn't work as well, because the in-class friend function is only findable via ADL. How might I call the non-member swap() from inside the class?

The problem is that the name of the member function swap hides the namespace-scope swap in the body of A::swap. Unqualified name lookup for swap in A::swap will never find namespace-scope swap and, thus, namespace-scope swap will not be part of the overload set. One way to get around this would be to simply add a declaration for namespace-scope swap in the body of A::swap:

class A
{
public:
    friend void swap(A& a, A& b) { /* swap the stuff */ }

    void swap(A& other)
    {
        void swap(A& a, A& b);
        swap(*this, other);
    }
};

That being said, I'm not sure what this really does for you. The obvious solution is to just implement namespace-scope swap in terms of A::swap rather than the other way around. Personally, I would just not have a swap member function to begin with. The typical way of swapping aand b is to just swap(a, b)

More C++ Idioms/Non-throwing swap, Although an efficient and exception-safe swap function can be implemented (as shown above) as a member function, non-throwing swap idiom goes further than � If your implementation of swap needs to access the type's privates, then you will either need to call a member function like #3, or declare the non-member function a friend. This is what your examples in #4 do: a friend function can be declared and defined inside the class, but that does not make it a member; it is still scoped within the

The following works for me (it prints blah twice)

#include <utility>
#include <iostream>

class A {
    public:
        friend void swap(A& a, A& b) { std::cout << "blah\n";/* swap the stuff */ }
        void swap(A& other) {  using std::swap; swap(*this, other); }
};

int main() {
    A a, b;
    swap(a,b);
    a.swap(b);
}

std::swap, Like for example std::begin(x) is better than x.begin(), std::size(x) is better than x. size(). Also functions like swap should be implemented as free functions and not as member I didn't see any uses of member swap that could be improved. Maybe 2020 GitHub, Inc. Terms � Privacy � Security � Status � Help. The launch date for Insolar MainNet was moved a few days forward because the developers need to implement the updated swap terms. Summary of updated swap terms. Become a member.

You can declare your class and your swap function first, then use the global namespace specifier within your member function.

Now you are free to define your swap function outside the class.

#include <algorithm>
#include <iostream>

class A;
void swap(A& a, A& b);

class A {
    int the_stuff;

  public:
    A(int a): the_stuff(a) {}
    friend void swap(A& a, A& b);
    void print_stuff(){std::cout << "A's stuff is " << the_stuff << std::endl;}
    void swap(A& other){
      ::swap(*this, other);
      std::cout << "Member swap" << std::endl;}
};

void swap(A& a, A& b)
{
  std::swap(a.the_stuff, b.the_stuff);
  std::cout << "Friend swap" << std::endl;
}

int main()
{
  A a = 1, b = 2;
  a.print_stuff();
  swap(a, b);
  a.print_stuff();
  return 0;
}

Outputs:

//> A's stuff is 1
//> Friend swap
//> A's stuff is 2

guideline for preferring free functions over member functions � Issue , The (pointer, count)-style interface leaves increment1() with no realistic way of Note that non- const member functions pass information to other member Consider using swap to implement copy assignment in terms of copy construction . Extendible swap. A swap in which one counterparty has the right to extend a swap beyond its original term. It is the combination of a vanilla swap with a swaption (payer or receiver) whose expiry date coincides with the maturity date of the existing interest rate swap. Most commonly it is the fixed-rate payer who has the option.

C++ Core Guidelines, In computing, sequence containers refer to a group of container class templates in the standard library of the C++ programming language that implement storage of data elements. 3.2 Non-member functions. 4 Usage example; 5 The reserve() operation may be used to prevent unnecessary reallocations. After a call to� Replacement Swap: A substitute for a swap arrangement that is terminated before it matures. A swap may be ended early if there is a termination event or a default. If a swap is terminated early

Sequence container (C++), Why am I getting errors when my template-derived-class uses a member it inherits from its template-base-class? Every time we used swap() with a given pair of types, the compiler will go to the above Conceptual only; not C++; case int: // implementation details when T is int; break;; case Terms of Use � Privacy Policy. The expected way to make a program-defined type swappable is to provide a non-member function swap in the same namespace as the type: see Swappable for details.. The following overloads are already provided by the standard library:

Templates, C++ FAQ, Conversely, this is not a problem for the swap member function, as the Here is a typical implementation of std::swap, with comments of we define the free- function swap in terms of the member-function, rather than the other way around. a.swap(b), void, Requires: a.get_allocator() == b.get_allocator()� Swap Documents means each document, instrument or agreement entered into by and between Borrower and the Bank to evidence, confirm, describe, secure, modify or establish the terms of any Swap Transaction effected pursuant to this Agreement, including without limitation any so-called "Master Agreement" concerning any Swap Transaction and each schedule and addendum thereto, together with all

Comments
  • Why not to implement the non-member swap in terms of the member swap?
  • @Evg I could, but the symmetry of the non-member swap looks nicer.
  • Why do you want to add the implementation of the swapping in the friend function void swap(A& a, A& b)? It should belong to a member function like void swap(A& other) and the void swap(A& a, A& b) then could call the member function.
  • Also, if you do it the proper way (implement the non-member using the member), you don't need that the non member would be a friend of the class. You are clearly inventing problems...
  • @Phil1970 Do you have a reference that says that implementing the non-member using the member is the proper way?
  • If your design encourages block-scope function declarations, you’re doing it wrong.
  • It might be worth saying why asking for the (non-customizable!) std::swap finds the hidden friend.
  • Note that friend swap in the question can benefit from being a hidden friend.