C++ 11 feature: auto and decltype

auto and decltype are the 2 new keywords that have been added in C++ 11. In this chapter we shall see how to use both of them.

auto: auto keyword is used to automatically infer the type of the variable.

decltype: decltype let us to extract the type of the variable and assign it to a variable.

auto keyword:

Befor to C++ 11, you need to declare the type of the variable used.

After C++ 11, we can use auto keyword to infer the type. The compiler will inter the type.

Because of this compilation time might increase. But run time will not be hurt.

You can use “auto” in below scenario:

1. declaring variable
2. Iterators
3. Function return type.

A function with an “auto” return type will be evaluated at run time.

Now we shall see series of example and understand “auto” keyword.

Note:
It is important to note that, if you use auto to declare a variable; then you need to initialize them.

Example 1:

Simple auto declaration:

#include <iostream>
//for more C++ tutorial visit www.ProDeveloperTutorial.com

using namespace std;

int main() 
{ 
    auto i =10;
    cout<<"Type is "<<typeid(i).name()<<endl;
} 

Output:

Type is i

Example 2:

auto for class object:

#include <iostream>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

class MyClass 
{ 
    
}; 
  

int main() 
{ 
    auto obj = new MyClass();
    cout<<"Type is "<<typeid(obj).name()<<endl;

} 

Output:

Type is P7MyClass

Example 3:

function returning auto:

#include <iostream>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

auto fun()
{
    auto a = 10;
    return a;
}

int main() 
{ 
    auto var = fun();
    cout<<"Type is "<<typeid(var).name()<<endl;

} 

Output:

Type is i

Example 4: using auto in iterators.

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

int main() 
{ 
    set<string> st; 
    st.insert({ "Pro", "Developer", "Tutorial", "com" }); 
  
    for (auto it = st.begin(); it != st.end(); it++) 
        cout << *it << " "; 
  
    return 0; 
}

Output:

Developer Pro Tutorial com

“decltype”

Sometimes you don’t know the type of an variable or expression, but you want to assign that type to a variable. In such cases we use decltype.

Syntax:

decltype(expression) variable_name;

decltype stands for declared type. If will analyze the declared type of “expression” and assign it to the variable_name.

Example 1:

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

int fun()
{
    return 10;
}

int main() 
{ 
    decltype(fun()) x;
    
    cout<<"Type is "<<typeid(x).name()<<endl;

    return 0; 
}

Output:

Type is i

Example 2:

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

int main() 
{ 
    int x;
    
    decltype(x) j = x + 40;
    
    cout<<"Type is "<<typeid(j).name()<<endl;

    return 0; 
}

Output:

Type is i
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *