11/10/2008

Inline or not, that's question

Today I did a code review that does the following:

void
Foo(
    const std::vector<T>  &v
)
{
    size_t count = v.size();
    ...
    // use count in several places
    ...
}

At first I wanted to point out that there is no point to caching the v.size() because:

  1. You really should expect optimizer to take care of such caching
  2. Function implemented in header files automatically inline by default

After I typed it in my review feedback I came to wonder: How sure I was for both of my points?  I know item #1 is purely an assumption and would really differ from compiler (and linker of course) to compiler, what about the 2nd point?

I thought I read that from MSDN but couldn't recover it to back me this time. I didn't want to spend more than it worth, but without written evidence I couldn't present this to my colleague, so I decided to take my comment back with some degree of unpleasant feeling of being defeated.

Is Function implemented in header files automatically become inline? My impression was challenged this time. Perhaps someday I will find some written document about it. Only the frustration of being unclear will made my feel bad for the rest of the day.

沒有留言: