C++中類型推斷(auto和decltype)的使用
類型推斷 是指在編程語言中自動推導(dǎo)表達(dá)式的數(shù)據(jù)類型。在C++11之前,每個數(shù)據(jù)類型都需要在編譯時顯示聲明,在運行時限制表達(dá)式的值,但在C++的新版本之后,引入了 auto 和 decltype等關(guān)鍵字,這允許程序員將類型推導(dǎo)留給了編譯器本身。
有了類型推斷功能,我們必須耗費時間去寫編譯器已經(jīng)知道的東西。由于所有類型僅在編譯器階段推導(dǎo),因此編譯時間略有增加,但不會影響程序的運行時間。
1. C++中的auto
C++中的 auto
關(guān)鍵字指定要聲明的變量的類型根據(jù)其初始值自動推導(dǎo)。如果函數(shù)的返回類型是auto,那么它將在運行時由返回類型表達(dá)式確定類型。auto的良好用途是在為容器創(chuàng)建迭代器時避免冗長的初始化。
注意:使用auto關(guān)鍵字聲明的變量應(yīng)僅在聲明時初始化,否則將出現(xiàn)編譯時錯誤。
C++中auto例子
// C++ program to demonstrate working of auto // and type inference #include <bits/stdc++.h> using namespace std; int main() { // auto a; this line will give error // because 'a' is not initialized at // the time of declaration // a=33; // see here x ,y,ptr are // initialised at the time of // declaration hence there is // no error in them auto x = 4; auto y = 3.37; auto z = 3.37f; auto c = 'a'; auto ptr = &x; auto pptr = &ptr; //pointer to a pointer cout << typeid(x).name() << endl << typeid(y).name() << endl << typeid(z).name() << endl << typeid(c).name() << endl << typeid(ptr).name() << endl << typeid(pptr).name() << endl; return 0; }
輸出
i
d
f
c
Pi
PPi
注意:我們使用了 typeid 來獲取變量的類型。
這里,typeid是一個運算符,用于獲取需要對象的動態(tài)類型。
typeid(x).name()
返回 x
的數(shù)據(jù)類型,例如,它返回:
i
表整數(shù)integer,d
表 doublesf
表 float,c
表char
Pi
表指向整數(shù)的指針Pd
表示指向double的指針Pc
表示指向 char 的指針PPi
表示指向整數(shù)的指針的指針- 單指針前綴
P
, - 雙指針用
PP
作為前綴,以此類推
但實際返回的名稱主要取決于編譯器。
注意:auto的良好用途是在為容器創(chuàng)建迭代器時避免冗長的初始化。
C++ auto關(guān)鍵字例子
// 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; }
輸出
for geeks org
注意:如果給auto賦值一個整型引用,它也會變成整型。要使其成為引用類型,我們使用auto &。
- 返回整數(shù)引用類型的函數(shù):
int& fun() {};
m
會默認(rèn)為 int類型而不是 int&類型:auto m = fun();
n
將是int&
類型因為使用了auto &:auto& n = fun();
2. C++中的decltype
在C++中,decltype關(guān)鍵字可以檢查實體聲明的類型或表達(dá)式的類型。‘auto’ 允許你聲明具有特定類型的變量,而decltype允許你從變量中提取類型,所以decltype是一種計算傳遞表達(dá)式類型的操作符。
下面解釋上述關(guān)鍵字及其用途:
例子
// 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; }
輸出
i
c
以下是演示decltype的使用的又一個示例,
// C++ program to demonstrate use of decltype #include <bits/stdc++.h> using namespace std; // Driver Code 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; }
輸出
i
示例:演示auto和decltype的使用的C++程序
下面是一個C++模板函數(shù)min_type()
,它返回兩個數(shù)字中的最小值。這兩個數(shù)字可以是任何整數(shù)類型。返回類型是由兩者中的最小值來確定的。
// 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 double type cout << findMin(4, 3.44) << endl; // This call returns 3 of double type cout << findMin(5.4, 3) << endl; return 0; }
輸出:
3.44
3
decltype vs typeid
以下是decltype和typeid之間的一些主要區(qū)別
- decltype在編譯時提供類型信息,而typeid在運行時提供。
- 因此,如果我們有一個基類引用(或指針)引用(或指向)一個派生類對象,decltype會將類型作為基類引用(或者指針),但typeid會將類型作為派生類引用(或者指針)。
到此這篇關(guān)于C++中類型推斷(auto和decltype)的使用的文章就介紹到這了,更多相關(guān)C++ 類型推斷 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT實戰(zhàn)之實現(xiàn)圖片瀏覽系統(tǒng)
這篇文章主要介紹了如何利用QT編寫一個圖片瀏覽系統(tǒng),可以支持自動播放,左右拖動切換,點擊列表切換,點擊按鈕切換等功能,感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04C++11運算符重載和向量類重載實例詳解(<<,>>,+,-,*等)
這篇文章主要給大家介紹了關(guān)于C++11運算符重載和向量類重載的相關(guān)資料,主要包括<<,>>,+,-,*等,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07Qt使用QChart實現(xiàn)靜態(tài)顯示溫度變化曲線
QChart模塊是Qt?Charts庫的基礎(chǔ),提供了用于創(chuàng)建和顯示各種類型圖表的類和接口,本文主要介紹了如何使用QChart實現(xiàn)動態(tài)顯示3個設(shè)備的溫度變化曲線,感興趣的可以了解一下2023-06-06c++指針參數(shù)傳遞和引用參數(shù)傳遞的區(qū)別解析
這篇文章主要介紹了c++指針參數(shù)傳遞和引用參數(shù)傳遞的區(qū)別解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07