Auto clasifier

Hi there!
I hope someone can help me with a little explanation for a clasifier.

The 'auto' specifier, how does it work and when would it be useful in terms of range based loops?

I have this basic code that we're currently looking at to understand it, but Im having trouble wrapping my head around whats its use.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <array>
using namespace std;

int main (){
  const int rows = 2; 

  const int columns = 3; 

  array<array<int, columns>, rows> A1 = {1, 2, 3, 4, 5, 6};
  //


    for (auto const row : A1){ 

//So its this part that I dont understand,whats the 'auto' for??

      for (auto const element : row) {
        cout <<element <<" ";
      }
      cout <<endl;
    }

  return 0;
}


Thank you in advance!!
https://en.cppreference.com/w/cpp/language/auto explains what the auto keyword does.

It's useful whenever you don't need to specify a specific type for a variable, when that type can be deduced from what you're using to initialise it.
Basically when you use the auto specifier, the compiler looks at the type of the value which was used to initialize the variable. And it declares the variable to be of the type of which the value that was assigned to it was.

So if you wrote:
auto foo = 5;

foo would be an integer variable.

Why is this useful? auto can also declare variables as objects so often it's just simply to improve readability. But auto can also be used in cases where the variable could be holding more than one possible type. Sometimes it's a matter of convenience, as it's much easier to type auto.

For the ranged for loop, auto gives row and element the value of integer. Here it can be handy as you can use the bit of code to display an array of anything. Of course you can explicitly declare row and element to be integer, but what if the array is not of integer type? Then you the ranged for loop would fail, on the other hand the ranged for loop with auto would handle this just fine.
Last edited on
@Nwb, you have so little experience you really need to test even the smallest piece of code you show to some unsuspecting fellow beginner. Your code example will not compile (obviously).
Lets fail:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <array>
using namespace std;

int main (){
  const int rows = 2; 
  const int columns = 3; 
  array<array<int, columns>, rows> A1 = {1, 2, 3, 4, 5, 6};
  for (auto const row : A1){ 
    decltype(row)::foo = 1;
    // error: 'foo' is not a member of 'std::array<int, 3ul>'
    for (auto const element : row) {
      decltype(element)::foo = 1;
      // error: decltype evaluates to 'const int', which is not a class or enumeration type
    }
  }
}


Again:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;

int main (){
  const map<string,vector<string>> snafu;

  for ( auto foo : snafu ) { 
    decltype(foo)::bar = 1;
    // error: 'bar' is not a member of 'std::pair<const std::basic_string<char>, std::vector<std::basic_string<char> > >'
  }
}

That went a bit overboard, but nevertheless:
1
2
3
for ( auto foo : snafu )
// vs.
for ( pair<const string, vector<string>> foo : snafu )

It is easy to get long typenames. You could use aliases, or you could use auto.

On the other hand, you need to know what foo is in order to use it.
Can you remember it from snafu, or do you have to spell it out?
its a tool :)
sometimes it makes code more readable and is handy, and sometimes it makes it harder to follow. If everything you type is of type auto, it can quickly get confusing as to what you have.
I really like autos for letting it decide which of the 8+ integer types is correct.

Topic archived. No new replies allowed.