神刀安全网

Type Inference in C++ (auto and decltype)

Type Inference refers to automatic deduction of data type of an expression in a programming language. Before C++ 11, each data type need to be explicitly declared at compile time, limiting the values of an expression at run time but after new version of C++ many keywords are included which allows a programmer to leave the type deduction to compiler itself.With type inference capabilities, we can spend less time having to write out things compiler already knows. As all the types are deduced in compiler phase only, the time for compilation increases slightly but it does not affect run time of program.

auto keywprd
The auto keyword specifies that the type of the variable that is begin declared will automatically be deduced from its initializer and for functions if their return type is auto then that will be evaluated by return type expression at runtime.

// C++ program to demonstrate working of auto // and type inference #include<bits/stdc++.h> using namespace std;  int main() {     auto x = 4;     auto y = 3.37;     auto ptr = &x;     cout <<  typeid(x).name() << endl          <<  typeid(y).name() << endl          << typeid(ptr).name() << endl;      return 0; }

Output :

i d  Pi

We have used typeid for getting the type of the variables. typeid is an operator which is used where dynamic type of an object need to be known. typeid(x).name() return short hand name of data type of x, for example it return i for integers, d for doubles, Pi for pointer to integer etc but actual name returned is mostly compiler dependent. You can read more about typeid here .

A good use of auto is to avoid long initializations when creating iterators for containers.

// C++ program to demonstrate that we can use auto to  // save time when creating iterators #include<bits/stdc++.h> using namespace std;  int main() {     // Create a set of strings     set<string> st;     st.insert({"geeks", "for", "geeks", "org"});      // 'it' evaluates to iterator to set of string     // type automatically     for (auto it = st.begin(); it != st.end(); it++)         cout << *it << " ";      return 0; }

Output :

for geeks org

Other thing to note about auto is, auto becomes int type if even an integer reference is assigned to it. To make it reference type, we use auto &.

// function that returns a 'reference to int' type int& fun() {   }  // m will default to int type instead of // int& type auto m = fun();  // n will be of int& type because of use of // extra & with auto keyword auto& n = fun();
decltype Keyword

It inspects the declared type of an entity or the type of an expression. Auto lets you declare a variable with particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression.

Explanation of above keyword and their uses is given below

// C++ program to demonstrate use of decltype #include<bits/stdc++.h> using namespace std;  int fun1()   { return 10;  } char fun2()  { return 'g'; }  int main() {     // Data type of x is same as return type of fun1()     // and type of y is same as return type of fun2()     decltype(fun1()) x;     decltype(fun2()) y;      cout <<  typeid(x).name() << endl;     cout <<  typeid(y).name() << endl;      return 0; }

Output:

i c

Below is one more example to demonstrate use of decltype

// Another C++ program to demonstrate use of decltype #include<bits/stdc++.h> using namespace std; int main() {    int x = 5;        // j will be of type int : data type of x    decltype(x) j = x + 5;     cout << typeid(j).name();     return 0; }

Output:

i c

A program that demonstrates use of both auto decltype.

Below is a C++ template function min_type() that returns minimum of two numbers. The two numbers can be of any integral type. The return type is determined using type of minimum of two.

// C++ program to demonstrate use of decltype in functions #include <bits/stdc++.h> using namespace std;  // A generic function which finds minimum of two values // return type is type of variable which is minimum template<class A, class B> auto findMin(A a, B b) -> decltype(a < b ? a : b) {     return (a < b) ? a : b; }  // driver function to test various inference int main() {     // This call returns 3.44 of doubale type     cout << findMin(4, 3.44) << endl;      // This call returns 3 of int type     cout << findMin(5.4, 3) << endl;       return 0; }

Output:

i3.44 3

This article is contributed byUtkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Type Inference in C++ (auto and decltype)

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮