1198. Container adaptor swap: member or non-member?

Section: 26.6 [container.adaptors] Status: C++11 Submitter: Pablo Halpern Opened: 2009-08-26 Last modified: 2016-02-10

Priority: Not Prioritized

View all other issues in [container.adaptors].

View all issues with C++11 status.

Discussion:

Under 26.6 [container.adaptors] of N2914 the member function of swap of queue and stack call:

swap(c, q.c);

But under 26.6 [container.adaptors] of N2723 these members are specified to call:

c.swap(q.c);

Neither draft specifies the semantics of member swap for priority_queue though it is declared.

Although the distinction between member swap and non-member swap is not important when these adaptors are adapting standard containers, it may be important for user-defined containers.

We (Pablo and Howard) feel that it is more likely for a user-defined container to support a namespace scope swap than a member swap, and therefore these adaptors should use the container's namespace scope swap.

[ 2009-09-30 Daniel adds: ]

The outcome of this issue should be considered with the outcome of 774 both in style and in content (e.g. 774 bullet 9 suggests to define the semantic of void priority_queue::swap(priority_queue&) in terms of the member swap of the container).

[ 2010-03-28 Daniel update to diff against N3092. ]

[ 2010 Rapperswil: ]

Preference to move the wording into normative text, rather than inline function definitions in the class synopsis. Move to Tenatively Ready.

[ Adopted at 2010-11 Batavia ]

Proposed resolution:

Change 26.6.4.1 [queue.defn]:

template <class T, class Container = deque<T> > 
class queue {
   ...
   void swap(queue& q) { using std::swap;
                          c.swap(c, q.c); }
   ...
};

Change 26.6.5 [priority.queue]:

template <class T, class Container = vector<T>, 
          class Compare = less<typename Container::value_type> > 
class priority_queue { 
    ...
    void swap(priority_queue& q); { using std::swap;
                                     swap(c, q.c);
                                     swap(comp, q.comp); }
    ...
};

Change 26.6.6.1 [stack.defn]:

template <class T, class Container = deque<T> > 
class stack {
   ...
   void swap(stack& s) { using std::swap;
                          c.swap(c, s.c); }
   ...
};