I know this sounds crazy, but ... once, long ago, I put a print statement into "tokens.size()" (i.e. in the method being called to exit a loop)  and I found that it was being called *every time through the loop*.  I think the compiler is assuming that the method has side effects (i.e. not that the size is changing, but rather, that perhaps size() doing i/o or setting/releasing locks, counting something in some static data member, taking semaphores, whatever -- side-effects)  and so the compiler calls size() every time through the loop.  As a result I now have this strange habit of writing  size_t arity = thing.size(); for (... ; I< arity; i++) ...