Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I came upon the auto keyword in c++ and i was wondering why not always use the auto keyword instead of using normal data types?

What I have tried:

i have tried to search something about it but no results explain it.
Posted
Updated 20-Jun-21 1:47am
v2
Comments
Richard MacCutchan 14-Apr-21 3:45am    
Because using the proper keywords has (at least) two benefits:
1. You think more about what your code is supposed to be doing.
2. Someone else reading it can get a better understanding of what it is doing.

You can use it a lot but I find that often the compiler can't discern the best type. There are also other cases where you want to be explicit about the type of data you are going to use so you can force the compiler to use it by being explicit. I use it most often when obtaining a value from a Get'er type method and with STL iterators.
 
Share this answer
 
Comments
Dave Kreskowiak 13-Apr-21 23:30pm    
Not using auto all the time also aids in the concept of self documenting code. You don't have to guess at what type is being used.
CPallini 14-Apr-21 1:46am    
5.
I will don my asbestos suit and offer a somewhat contrary opinion.

I use auto on almost every possible occasion. Why? It keeps the code tight rather than spilling lines when a long type name would otherwise have to replace auto. But that's only the first reason.

Not using auto in order to make the code easier to understand can easily lend a false sense of security. Why is someone looking at my function's implementation? If it's because my interface didn't document it well enough, that is what needs to be fixed. But if the interface is OK, they're looking at the implementation because there are details that they need to understand. Maybe they think there's a bug, in which case they might even need to fix it.

So if they see auto and don't know what type it represents, it means they don't understand the right-hand side of the expression that is imputing its type to the left-hand side. Quite often, the right-hand side is a function call. In which case it means they don't understand what that function does, so they'd better investigate if they want to truly understand what's going on. It doesn't really help to tell them what the type is, because they need to look at that function's documentation and maybe even its implementation.

Just be sure you know what type auto will assign. One of the few places I specify the actual type is in this kind of loop:
C++
for(size_t i = 0; i < vec.size(); ++i)...
Here, if you use auto for i, you get an int (the default type for 0). But size() returns size_t. That's an unsigned int, so you have to specify size_t explicitly.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900