Harrison Tran
Harrison Tran

Reputation: 1621

C++ auto keyword for flexibility

Is it an good idea to use auto as much as possible so changing data types in your code is more flexible? Like if you use a bunch of range-based for loops would it be efficient to always use auto so you never have to go back and change the data type the for loop?

Upvotes: 0

Views: 85

Answers (1)

Humam Helfawi
Humam Helfawi

Reputation: 20274

In short, yes. See this for details AAA. However please be aware of some cases like this:

int x = 4;
int& ref = x;
auto y = ref;

y now is int not int&

const int x = 5;
auto y = x;

y is int not const int;

Upvotes: 1

Related Questions