[Bug libstdc++/48038] [C++0x] stable_sort problem with C++0x and comparator by value

vincenzo.innocente at cern dot ch gcc-bugzilla@gcc.gnu.org
Wed Mar 9 16:01:00 GMT 2011


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

--- Comment #28 from vincenzo Innocente <vincenzo.innocente at cern dot ch> 2011-03-09 16:01:33 UTC ---
The use case was "sloppy" coders (we have hundreds of them, a real resource to
find bugs!)

I personally have no problem with "const &".
Still I remember many tutorials and code examples that suggest that smart
pointers should be passed by value.
"const&" is often perceived as an implementation detail by many non
professional software developers that would like to see those details hidden.
Smart pointers were seen as one of the way to hide memory management from them. 
Move semantics gives new opportunities to move optimization away from user
code: this is one of the reason we are pushing C++0x and we would really like
to be usable now.

vincenzo


here is the code for the shared pointer.

#include<memory>
#include<vector>
#include<functional>

#include<iostream>
#include<algorithm>


struct A {
  A(int ii=-1) : i(ii){}
  int i;
};

typedef std::shared_ptr<A> Aptr;

struct Aless {
  bool operator()(Aptr lh, Aptr rh) {
    return lh->i < rh->i;
  }
};

int main() {

  std::vector<Aptr> v;
  v.push_back(Aptr(new A(2)));
  v.push_back(Aptr(new A(0)));
  v.push_back(Aptr(new A(1)));
  v.push_back(v[1]);

  for (auto p=v.begin(); p!=v.end(); ++p)
    std::cout <<  (*p)->i << " ";
  std::cout << std::endl;


  std::stable_sort(v.begin(),v.end(),Aless());

  for (auto p=v.begin(); p!=v.end(); ++p)
    std::cout <<  (*p)->i << " ";
  std::cout << std::endl;

}

On 9 Mar, 2011, at 4:33 PM, paolo.carlini at oracle dot com wrote:

> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48038
> 
> --- Comment #23 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-03-09 15:33:33 UTC ---
> Vincenzo no hurry, thankfully Chris has a patch essentially ready (apparently)
> and most likely we can fix the problem over the next hours. But in general I'm
> unconvinced that it makes sense for a comparison operator of class types to use
> pass by value. I would be interested in understanding more about your use case
> (maybe in private, whatever you prefer)
> 
> -- 
> Configure bugmail: http://gcc.gnu.org/bugzilla/userprefs.cgi?tab=email
> ------- You are receiving this mail because: -------
> You reported the bug.

--
Il est bon de suivre sa pente, pourvu que ce soit en montant. 
A.G.
http://www.flickr.com/photos/vin60/1320965757/



More information about the Gcc-bugs mailing list