Yes, I know iit does call size() every time, I also sometimes perform this optimization.

C++ sucks a bit in that respect, it doesn't have anyway to tell that a function is pure (I guess in principle it could infer that in determined cases but it's probably too hard in practice). D however has a 'pure' statement that compile-guaranties no IO, random, etc. D rocks IMO!