This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of TC1 status.

227. std::swap() should require CopyConstructible or DefaultConstructible arguments

Section: 26.7.3 [alg.swap] Status: TC1 Submitter: Dave Abrahams Opened: 2000-04-09 Last modified: 2016-01-28

Priority: Not Prioritized

View all other issues in [alg.swap].

View all issues with TC1 status.

Discussion:

25.2.2 reads:

template<class T> void swap(T& a, T& b);

Requires: Type T is Assignable (_lib.container.requirements_).
Effects: Exchanges values stored in two locations.

The only reasonable** generic implementation of swap requires construction of a new temporary copy of one of its arguments:

template<class T> void swap(T& a, T& b);
  {
      T tmp(a);
      a = b;
      b = tmp;
  }

But a type which is only Assignable cannot be swapped by this implementation.

**Yes, there's also an unreasonable implementation which would require T to be DefaultConstructible instead of CopyConstructible. I don't think this is worthy of consideration:

template<class T> void swap(T& a, T& b);
{
    T tmp;
    tmp = a;
    a = b;
    b = tmp;
}

Proposed resolution:

Change 25.2.2 paragraph 1 from:

Requires: Type T is Assignable (23.1).

to:

Requires: Type T is CopyConstructible (20.1.3) and Assignable (23.1)