title: When do you require to use decltype in C++
body: <p>I am learning C++ and have found the following code in C++ primer:</p>
  
  <pre><code>int main() {
  
  string s("some string");
  for(decltype(s.size()) index = 0; index != s.size() &amp;&amp; !isspace(s[index]); ++index)
      s[index] = toupper(s[index]);
  
  cout &lt;&lt; s;
  </code></pre>
  
  <p>}</p>
  
  <p>I understand the code capitalized the first word in the string and the output is </p>
  
  <pre><code>SOME string
  </code></pre>
  
  <p>What I am curious is that why do you need to use <code>decltype(s.size())</code> to declare the type for index. If I change it to <code>int index =0</code>, the code still compile and run with no problem. It seems easier for me to use <code>int</code>. I think I am missing some import concept here, any feedback will be really helpful. Thanks!</p>