[Bug libstdc++/38466] New: std::swap does not use std::swap for the components of a std::pair

bartoschek at gmx dot de gcc-bugzilla@gcc.gnu.org
Tue Dec 9 22:55:00 GMT 2008


Sorting a std::vector<std::pair<std::string, int> > is very slow because
std::swap is not used for switching strings. 

std::swap for every std::pair should use two std::swap for the components of
the pair.

The following test program shows that this is currently not the case:

#include <iostream>
#include <algorithm>
#include <vector>

class A {
public:
   A(int a) : val(a) { std::cout << "Constructing with: " << a << "\n"; }
   A(A const & a) : val(a.val) { std::cout << " Copy constructor from: " <<
a.val << "\n"; }

   A & operator=(A const & a) { val = a.val; std::cout << "Assignment from: "
<< a.val << "\n"; return *this; }

   ~A() { std::cout << "Destructing " << val << "\n";}

public:
   int val;

};

namespace std {

void swap(A & a, A & b) {
   std::cout << "SWapping: " << a.val << "/" << b.val << "\n";
   std::swap(a, b);
}

}

int main() {

   std::pair<A, int> f(A(10), 2);
   std::pair<A, int> s(A(20), 3);

   std::swap(f, s);
}


-- 
           Summary: std::swap does not use std::swap for the components of a
                    std::pair
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: bartoschek at gmx dot de
 GCC build triplet: i586-suse-linux
  GCC host triplet: i586-suse-linux
GCC target triplet: i586-suse-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38466



More information about the Gcc-bugs mailing list