Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!bionet!agate!ucbvax!pasteur!galileo.berkeley.edu!jbuck From: jbuck@galileo.berkeley.edu (Joe Buck) Newsgroups: comp.lang.c++ Subject: Re: why is this program slow? Message-ID: <10214@pasteur.Berkeley.EDU> Date: 15 Jan 91 18:43:24 GMT References: <1991Jan9.002244.23398@news.cs.indiana.edu> <1738@rose.ACA.MCC.COM> <1877@ruunsa.fys.ruu.nl> Sender: news@pasteur.Berkeley.EDU Reply-To: jbuck@galileo.berkeley.edu (Joe Buck) Lines: 36 In article <1877@ruunsa.fys.ruu.nl>, muts@fysaj.fys.ruu.nl (Peter Mutsaers /1000000) writes: |> It is a pity that the c++ optimizer does not (yet) automatically |> change a = a + b into a+=b if operator+= is defined too. It would |> reduce however the freedom to use operators like += *= etc. I got this trick from Andy Koenig. Whenever I write a class with arithmetic operators, I always write +=, *=, etc, as member functions and then write (let's say the class is Foo) inline Foo operator+ (const Foo& a, const Foo& b) { Foo tmp(a); tmp += b; return tmp; } Notice that this function does not need to be a friend (so when people ask me whether arithmetic operators should be members or friends, I reply, neither). With this kind of structure, most optimizers can convert func (Foo& x, Foo& y) { ... Foo c = x + y; ... } into Foo c(x); c += y; with little trouble. -- Joe Buck jbuck@galileo.berkeley.edu {uunet,ucbvax}!galileo.berkeley.edu!jbuck