vendredi 25 septembre 2015

What happens when one uses auto with two declarations in C++11?

Let's say there is a loop like this:

for(size_t i=0, n=ar.size(); i<n; ++i)
{
  // ...
}

Would it be good to rewrite it as:

for(auto i=0, n=ar.size(); i<n; ++i)
{
  // ...
}

In other words will the two variables i and n always end up being the same datatype.

When i try something like this:

auto i=0, s="";

g++ 4.8.4 generates an error inconsistent deduction for ‘auto’: ‘int’ and then ‘const char*’. But i cannot find out if it's just g++ or this is actually required according to the standard to use every value in the type deduction.

Aucun commentaire:

Enregistrer un commentaire