Title
std::swap() should require CopyConstructible or DefaultConstructible arguments
Status
tc1
Section
[alg.swap]
Submitter
Dave Abrahams

Created on 2000-04-09.00:00:00 last changed 172 months ago

Messages

Date: 2010-10-21.18:28:33

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)

Date: 2000-04-09.00:00:00

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;
}
History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg1924
2000-04-09 00:00:00admincreate