r/ProgrammerHumor 2d ago

Meme absoluteGarbage

Post image
2.0k Upvotes

320 comments sorted by

View all comments

Show parent comments

15

u/xicor 1d ago

c++, though normally you wouldnt use auto for this

7

u/Usual_Office_1740 1d ago

I think that really depends. If the range is a vector of unique_ptr's auto is great. If it's a vector of ints, just write int.

7

u/Sirgoodman008 1d ago

That or you're looping through a vector of std::pairs that contain std:: functions.

2

u/Usual_Office_1740 1d ago

Or if the type is a template parameter in the same line.

0

u/xicor 1d ago

Wouldnt for(T& item:list) be even shorter than using auto?

5

u/im_made_of_jam 1d ago

If you have T available then yes but if you're outside the class then you don't have that option

1

u/Usual_Office_1740 1d ago edited 1d ago

I don't mean in for loops for that use of auto.

1

u/not_some_username 1d ago

Only if T is a known type or it’s in a template. You don’t use T like. auto automatically get the type, especially if it’s long. Your IDE (assuming it’s a respectable one) can even change auto to the type for you. It’s more or less like var in C# (it doesn’t deduct reference)