[PATCH] Exploit move semantics in std::accumulate.

Marc Glisse marc.glisse@inria.fr
Tue Jan 11 12:45:00 GMT 2011


On Tue, 11 Jan 2011, Paolo Carlini wrote:

> ... for concreteness, this is what I actually tested.

Why use x=move(x)+y and not x+=y?

> Seems safe to me, being restricted to string / wstring (eg, no problem 
> with basic_string specializations), I can't see how the user could 
> detect the optimization.

It would be safe if you also restricted the iterator's reference type. I 
believe a user can define:

std::string operator+(std::string const&,MyType){...}
std::string operator+(std::string&&,MyType)=delete;

I guess in practice I'll never user accumulate anyway (I have never used 
it yet), more like (untested and not thought long about):
typedef typename iterator_traits<Iter>::reference&& T;
foreach(begin,end,[&acc](T x){
  acc+=static_cast<T>(x); })


-- 
Marc Glisse



More information about the Libstdc++ mailing list