今天就跟大家聊聊有關(guān)C++中怎么使用auto避免多余的類型名重復(fù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)公司是一家專業(yè)從事成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計的品牌網(wǎng)絡(luò)公司。如今是成都地區(qū)具影響力的網(wǎng)站設(shè)計公司,作為專業(yè)的成都網(wǎng)站建設(shè)公司,創(chuàng)新互聯(lián)公司依托強大的技術(shù)實力、以及多年的網(wǎng)站運營經(jīng)驗,為您提供專業(yè)的成都網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)及網(wǎng)站設(shè)計開發(fā)服務(wù)!
Simple repetition is tedious and error-prone.
簡單的重復(fù)多余且易錯。
When you use auto, the name of the declared entity is in a fixed position in the declaration, increasing readability.
當(dāng)你使用auto的時候,被定義實體的名稱會出現(xiàn)在固定的位置,這樣可以增加可讀性。
In a template function declaration the return type can be a member type.
在模板函數(shù)定義中,返回值可以是成員類型。
Consider:
考慮以下代碼:
auto p = v.begin(); // vector<int>::iterator
auto h = t.future();
auto q = make_unique<int[]>(s);
auto f = [](int x){ return x + 10; };
In each case, we save writing a longish, hard-to-remember type that the compiler already knows but a programmer could get wrong.
無論哪種情況,我們都不必編寫又長、類型又難記的類型信息。其實這些信息編譯器已經(jīng)知道,但程序員還是會弄錯。
Example(示例)
template<class T>
auto Container<T>::first() -> Iterator; // Container<T>::Iterator
Avoid auto for initializer lists and in cases where you know exactly which type you want and where an initializer might require conversion.
對于你確切地知道所需類型但初始化器可能需要轉(zhuǎn)換的情況,應(yīng)避免為初始化列表使用auto。
Example(示例)
auto lst = { 1, 2, 3 }; // lst is an initializer list
auto x{1}; // x is an int (in C++17; initializer_list in C++11)
When concepts become available, we can (and should) be more specific about the type we are deducing:
在concepts可以之后,我們可以(也應(yīng)該)更加明確我們推斷的類型。
// ...
ForwardIterator p = algo(x, y, z);
示例(C++17)
auto [ quotient, remainder ] = div(123456, 73); // break out the members of the div_t result
Flag redundant repetition of type names in a declaration.
標(biāo)記在聲明時發(fā)生的多余的類型名稱重復(fù)。
看完上述內(nèi)容,你們對C++中怎么使用auto避免多余的類型名重復(fù)有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)站名稱:C++中怎么使用auto避免多余的類型名重復(fù)
鏈接地址:http://chinadenli.net/article12/gisgdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、、網(wǎng)站改版、動態(tài)網(wǎng)站、定制網(wǎng)站、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)