Abdelrazak Younes schrieb:
Jean-Marc Lasgouttes a écrit :
Le 20 avr. 09 à 00:25, Jean-Marc Lasgouttes a écrit :
Le 19 avr. 09 à 23:13, Abdelrazak Younes a écrit :
+ docstring const fls = flattenLabelString(it->first, false, callers);
docstring const & ?

Indeed, thanks.

Now that I think of it, how come that we have lots of
  string const foo = bar();
expressions in the code? Adding a "&" seems to save a copy, but what does the compiler do
behind the scenes?

AFAIK the copying strings normally does not copy the data until the source or the destination string is changed. Thus copying strings is not that expensive an many cases.

This essentially depends on bar(). If bar() generates a new string, it should of course return a copy and you should not use a const ref. If bar() returns a reference or a const reference to an internal string, then you can save a copy by using a const ref too.

The compiler (at least some compilers) issue a warning for these cases. E.g.

struct T {
   int x;
};

const T &Foo() {
   T t;
   t.x = 1;
   return t;
}
...

> g++ test.cc -o test
test.cc: In function »const T& Foo()«:
test.cc:6: Warnung: Referenz auf lokale Variable »t« zurückgegeben

bernhard

Reply via email to