This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Remove algo code duplication
- From: Marc Glisse <marc dot glisse at inria dot fr>
- To: François Dumont <frs dot dumont at gmail dot com>
- Cc: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>
- Date: Wed, 2 May 2012 22:10:07 +0200 (CEST)
- Subject: Re: Remove algo code duplication
- References: <4F9BA3CF.2000606@gmail.com> <alpine.DEB.2.02.1204281307410.2528@laptop-mg.saclay.inria.fr> <4FA18C34.7070004@gmail.com>
- Reply-to: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>
[Jonathan already answered most of it]
On Wed, 2 May 2012, François Dumont wrote:
2. Compatible with existing code, code submitted by Christopher last time
will still compile. To do so I used std::iterator_traits<>::reference type
so that if the iterator is not const then the operator do not have to take
const reference or to be const qualified neither. For the same reason I
have also avoided some const qualifiers on the introduced functors.
It probably doesn't matter, but the difference with the current code can be
seen. With -fno-elide-constructors you perform copies, operator< is always
given an lvalue, etc.
You mean functor copies, right ? I expect the compiler to optimize them away
even if there are options to forbid him to do so.
I mean value copies, performed by the functor.
#include <iostream>
struct A {
A(){std::cout << "A()\n";}
A(A const&){std::cout << "A(A const&)\n";}
A(A &&){std::cout << "A(A &&)\n";}
};
bool operator<(A,A){return true;}
struct less {
bool operator()(A a,A b)const{return a<b;}
};
struct Iter {
A operator*()const{return A();}
};
int main(){
Iter x;
std::cout << "hello\n";
*x < *x; // all copies elided
std::cout << "pause\n";
less()(*x,*x); // 2 copies can't be elided
std::cout << "bye\n";
}
hello
A()
A()
pause
A()
A()
A(A const&)
A(A const&)
bye
I am not saying it is necessarily a bad thing, just that the difference is
noticable. Note that the functor less posted by Jonathan would keep 2 move
constructors instead of the 2 copy constructors.
(note: I am not sure that's what I had in mind when I wrote that email, I
can't remember exactly)
--
Marc Glisse