C++11之前数据类型在编译的时候必须显示指定, 在新版本的C++中, 许多关键字可以让编译器自动推导出数据类型, 虽然这会略微增添编译的时间, 但对最终的程序的运行时间没有任何影响, 这给编写程序带来了很大的便利.
-
auto
用于声明变量时, 根据变量的值推导出变量的类型
示例1:// 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; }
在使用复杂的变量类型声明变量时尤其方便
示例2:// 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; }
引用类型的自动推导需要用
auto &
, 也就是说auto自动推导类型时会忽略掉&
, 引用类型需要手动加上// fun为返回int型引用的函数 int& fun() { } // m 为 int 型 // int& type auto m = fun(); // auto 加上 & 后, n 为 int& 型 auto& n = fun();
-
decltype
decltype用抽取实体或者表达式的类型, 这个类型可以像其他数据类型一样使用, 而不是想auto一样在定义时根据定义的值自动推导出数据类型.
示例1:// 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; }
示例2:
// 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; }
使用关键字
decltype
的时候,左值和右值有所不同。如果表达式的求值结果是左值,decltype
作用于该表达式(不是变量)得到一个引用类型,例如,假定p的类型是int*
,因为解引用运算符生成左值,所以decltype(*p)
的结果是int&
。另一方面,因为取地址运算符生成右值,所以decltype(&p)
的结果是int**
,也就是说,结果是一个指向整型指针的指针。double aa = 3.444; double *m = &aa; decltype(&m) x; decltype(*m) y = *m; cout << "decltype(&m) type is : " << typeid(x).name() << endl; cout << "decltype(*m) type is : " << typeid(y).name() << endl;
输出:
decltype(&m) type is : PPd decltype(*m) type is : d
综合例子:
// 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 // 尾置返回类型 "auto func()->int" 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 auto a = findMin(4, 3.44); cout << a << " " << typeid(a).name() << endl; // This call returns 3 of double type auto b = findMin(5.4, 3); cout << b << " " << typeid(b).name() << endl; double i = 3.444; int j = 6; auto c = (i > j ? i : j); cout << c << " " << typeid(c).name() << endl; return 0; }
两个不同类型的数字比较会做隐式类型转换, 这里的double和int比较, 最终的结果是double型.
输出:pam:~/cplus/auto_deduction$ ./decltype 3.44 d 3 d 6 d
https://blog.csdn.net/qq_41453285/article/details/91895105
https://www.geeksforgeeks.org/type-inference-in-c-auto-and-decltype/
https://www.cnblogs.com/golaxy/p/9212897.html