> Your first example points to a weakness in the compiler optimization.
> If base_string constructor is inlined, the compiler should be able to
> figure out both 'name' and the heap memory it points to can not be
> modified by the call to notify, and therefore hoist access name.c_str
> () and name.length () out of the loop. Even without inlining, with
> more powerful modeling of standard function side effect (e.g.,
> base_string ctor does not expose name's address), the same
> optimization should be performed.

You're right, 4.5.1 and current trunk appear to correctly hoist those accesses 
out of the loop.

It would still be useful for codebases that are built using different compilers 
that don't have this kind of optimization, though.

> > Example:
> >
> > #include <string>
> > #include <cstdio>
> >
> > void notify(const char* printable) { printf("%s\n", printable); }
> >
> > int main(void)
> > {
> >  std::string name("bob");
> >  for (int i = 0; i < name.length(); i++)
> >  {
> >    notify(name.c_str());
> >  }
> >
> >  return 0;
> > }

Reply via email to