if we went through the previous articles we have been talking about the type deduction with templates, declaring type without the impact of type deduction rules, and lastly how to know the type of objects. Now, let's talk briefly about the auto type and why we need to replace the type interface with auto. if we take the example of initialize x; we would write it
int x;
if it is left without initializing, we leave it to the compiler whether to set it to zero or any random value depending on the context. this sound like not the best practice.
int x1; // potentially unitizalied
auto x2; // error intailaizer required
auto x3=0; // now x3 is well defined
the above example shows one of the most benefits of auto, the compiler releases an error instead of acting unexpectedly, which in many cases is less dangerous.
Function pointer
before we dive into the function pointer auto can be used to represent types that are only known to the compiler.
auto func=[](const std::unique_ptr<widget>&p1,const std::unique_ptr<widget>&p2){return *p1<*p2;};
// can be more readble and converted into
auto func=[](const auto& p1,const auto&p2){return *p1<*p2;};
you may start to recall the std::function that holds the objects with closure. but let me tell you that std::function has proven to be slow, consumes more memory, and is even not friendly looking. but you might not be fully aware of the std::function
function pointer
From the name of the header, the function pointer is a pointer point only to a function. as for any type of pointer we have to declare the type of the pointer in our case we need to specify the type of the function pointer.
std::function<bool(const std::unique_ptr<widget>&,const std::unique_ptr<widget>&) func=[] (const std::unique_ptr<widget>&p1,const std::unique_ptr<widget>&p2){return *p1<*p2;};
and now the question is if we invoked auto, we will get the same result? the answer is now because auto uses only as much memory as the closure requires. while the std::function template has a fixed size and when it is not adequate the std::function creates a heap for the closure and the instance of std::function in the type declaration of the function. so I was right and my claim was accurate!!
the advantages of auto
the advantages of auto exceed the avoidance of uninitialized vars, var declaration, and the ability to hold closures. besides all of that, it seems problem of type shortcuts. this happens quite often when you declare a variable with none-fixed size based on the windows bits 64 or 32. like unsigned and size_type. while both are the same on 32 bits but they are different on 64 bits which may result in undefined behavior.
std::vector<int>v;
unsigned sz=v.size(); // the return of v.size() is sidze_type which is the same as unsigned in case of 32 bits but diffrent when it is 64 bits.
auto sz=v.size() // the type of sz is now type_size.
std::map<std::string,int>m;
for(const std::pair<std::string,int>&p:m)
// the type of pairs in mp is pair<const std::string,int> this is not the declared above in the iteration and for the compiler to run in such a way it will creat a temproray object from each pair in the map and that temp object will bedestroyed after each iteration, so if we stored the address of the temp object which is going to be destroyed by the end of the loop then you will be surprised.
for(const auto&p:m)
// you are sure this more efficnet and even more you know for sure this a pointer to an elemnt within m
This is not only more efficient, but it’s also easier to type. Furthermore, this code has the very attractive characteristic that if you take p’s address, you’re sure to get a pointer to an element within m. In the code not using auto, you’d get a pointer to a temporary object—an object that would be destroyed at the end of the loop iteration. what auto does is remove the overhead of mismatching type.
Disadvantages of Auto
you have been waiting for me to be natural about my topic but honestly, I am not. Auto has no disadvantages rather than a set of rules- type deduction. but like many dynamically typed language-python, java scripts. the type interface is not there, leaving the programmer to recognize the code from first glance to the evolution theory. then why not used in cpp?, another thing to mention the auto is used with the return type of object imagine a function used all over your code and uses the type int, for a scaling project you decided to change the function to use long instead in case of using explicit type then you have to change all calls of the function on the code. this sounds frustrating and I am too lazy to do so.
Things to remember
auto variables must be initialized, are generally immune to type mismatches that can lead to portability or efficiency problems, can ease the process of refactoring, and typically require less typing than variables with explicitly specified types.
auto-type vars are subjected to the pitfalls.