Home >>Cpp Programs >Auto keyword in Cpp
C++ auto is a type of keyword that exists and the function of it is to specify the type of the variable that is being declared should get deducted from its initialize. These are the keywords that are known as the Type Inference in C++. This basically refers to the automatic deduction of the data type of an expression that is in a programming language. Before the C++ 11 came in to existence, each of the data type needed to be explicitly declared at the compiling time that too being limiting the values of an expression at the runtime but that was the situation of previous times, after the new version of C++ has been released there are many keywords that has been included in the library that permits a programmer that they can leave the type deduction to the compiler itself.
The programmer now has to spend less time in writing the things that are already known to the compiler with the help of type inference capabilities. The time for compilation generally increases slightly because all the types are deduced only in the compiler phase despite of that there is no affect on the run time of the program. The auto keyword in C++ is associated with the decltype keyword and it is the joint companion of it. Let’s discuss both of them along with an example.
The auto keyword in C++ generally specifies that the type of the variable that is basically being declared will be deducted automatically from its initializer. Whereas, when it comes to functions and their return type is auto then the things will be different as that will be evaluated by the return type expression at the runtime.
Here is an example of the auto keyword that is depicted below and that will explain you about the application aspect of it:
#include <bits/stdc++.h> using namespace std; int main() { auto a = 10; auto b = 4.45; auto ptr = &a; cout << typeid(a).name() << endl << typeid(b).name() << endl << typeid(ptr).name() << endl; return 0; }
In the above example, there is the use of typeid just for getting the type of the variables. typeid is an operator in C++ that is used where the dynamic type of an object is needed to be known.
#include <bits/stdc++.h> using namespace std; int main() { //First need to Create a strings set set<string> str; str.insert({ "This", "is", "auto", "keyword" }); for (auto it = str.begin(); it != str.end(); it++) cout << *it << " "; return 0; }
decltype Keyword is C++ is generally used to inspect the declared type of an entity or the type of an expression that are used in the C++ programming language. Auto keyword in C++ basically lets the programmer to declare a variable with the particular type on the other hand the decltype keyword in C++ basically lets the programmer to extract the type direct from the variable hence; it can be said that the decltype is sort of an operator that generally evaluates the type of the expression that has been passed.
Here is an example of the same that will let you understand the application aspect of it:
#include <bits/stdc++.h> using namespace std; int Myfun1() { return 100; } char Myfun2() { return 'e'; } int main() { decltype(Myfun1()) a; decltype(Myfun2()) b; cout << typeid(a).name() << endl; cout << typeid(b).name() << endl; return 0; }