title: Why does using C++ iterators increase code size drastically compared to at() or indexing?
body: <p>I have been looking at using the newer C++ language features such as iterators on embedded systems (16KB of SRAM and 64 KB of flash, Cortex M4) and hit a surprising roadblock. Why on earth are iterators so monstrously large? I was under the impression they are basically some pointer arithmetic or indexing. Is the STL pulling in some unexpected code?</p>
  
  <p>These are using Kinetis Design Studio on windows with the gcc-arm-none-eabi-4_9 toolchain from <a href="https://launchpad.net/gcc-arm-embedded/+download" rel="nofollow">here</a> using the following flags.</p>
  
  <pre><code>arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fsingle-precision-constant -flto  -g3 -I"../Sources" -I"../Includes" -std=gnu++11 -fabi-version=0 -std=c++11 -MMD -MP -MF"Sources/System.d" -MT"Sources/System.o" -c -o "Sources/System.o" "../Sources/System.cpp"
  </code></pre>
  
  <p>ITM_SendChar just takes a single char and puts it in a register.</p>
  
  <pre><code>std::string input = "Oh hai there! :D\n";
  
  #ifdef char_array
      // .text              7352
      // .data               376
      // .bss                236
      for(int i = 0; i &lt; input.size(); i++)
              ITM_SendChar(input[i]);
  #endif
  
  #ifdef char_at
      // .text              7392
      // .data               376
      // .bss                236
      for(int i = 0; i &lt; input.size(); i++)
          ITM_SendChar(input.at(i));
  #endif
  
  #ifdef char_itterator
      // .text             39744        
      // .data               384   
      // .bss                252   
      for(char some_char : input)
          ITM_SendChar(some_char);
  #endif
  
  #ifdef char_itterator_auto
      // .text             39744        
      // .data               384   
      // .bss                252   
      for(auto some_char : input)
          ITM_SendChar(some_char);
  #endif
  
  #ifdef char_itterator_auto_no_copy
      // .text             39744        
      // .data               384   
      // .bss                252   
      for(auto&amp; some_char : input)
          ITM_SendChar(some_char);
  #endif
  </code></pre>