最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c++ decltype關(guān)鍵字的用法

 更新時間:2020年10月20日 10:51:30   作者:半杯茶的小酒杯  
這篇文章主要介紹了c++ decltype關(guān)鍵字的用法,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下

1. decltype關(guān)鍵字的用途是什么

給定變量的名稱或者表達(dá)式,decltype返回變量或者表達(dá)式的類型。如下所示:

const int i = 0; // decltype(i) is const int

bool f(const Widget& w); // decltype(w) is const Widget&,decltype(f) is bool(const Widget&)

struct Point {

int x, y; // decltype(Point::x) is int, decltype(Point::y) is int

};

Widget w; // decltype(w) is Widget

if (f(w)) ... // decltype(f(w)) is bool

template<typename T>class vector {

public:

...

T& operator[](std::size_t index);...

};

vector<int> v; // decltype(v) is vector<int>

if (v[0] == 0) ... // decltype(v[0]) is int&

2.decltype主要應(yīng)用場景是模板函數(shù)

decltype在實際的開發(fā)中主要用于模板函數(shù)中,函數(shù)的返回值依賴于模板參數(shù)類型的情況。如下authAndAccess函數(shù)的返回值類型依賴于Container的元素類型。

template<typename Container, typename Index>
auto authAndAccess(Container& c, Index i) -> decltype(c[i]) {

 authenticateUser();

 return c[i];
}

此處的返回值auto并非類型推導(dǎo)的意思,而是C++ 11中的函數(shù)返回類型后置的表達(dá)方式,表明函數(shù)的返回類型在參數(shù)列表之后。函數(shù)返回類型后置的優(yōu)勢在于我們可以用函數(shù)的參數(shù)來指定返回值。

在c++ 14中auto關(guān)鍵字可以獨立用于對函數(shù)的返回值進(jìn)行類型推導(dǎo),而不必采用c++ 11中的返回返回類型后置的聲明方式:

template<typename Container, typename Index>
auto authAndAccess(Container& c, Index i) {

 authenticateUser();

 return c[i]; // return type deduced from c[i]
}

但上述寫法在實際應(yīng)用針對具體case可能存在問題,比如如果operator[]返回T&,auto的推導(dǎo)機(jī)制會返回T,下面的就會編譯失敗:

std::deque<int> d;

...

authAndAccess(d, 5) = 10; //return d[5], then assign 10 to it; this won't compile!

因為根據(jù)auto的推導(dǎo)機(jī)制,authAndAccess返回的是右值,所以編譯不通過。authAndAccess函數(shù)需要聲明為如下方式才可以保證該示例編譯通過。

template<typename Container, typename Index>
decltype(auto) authAndAccess(Container& c, Index i){

 authenticateUser();

 return c[i];
}

decltype(auto)不僅僅可以用于函數(shù),也可以用于變量,可以完美推導(dǎo)變量的類型。

Widget w;
const Widget& cw = w;
auto myWidget1 = cw; // auto type deduction: myWidget1's type is Widget

decltype(auto) myWidget2 = cw; // decltype type deduction: myWidget2's type is const Widget&

再回到authAndAccess函數(shù)

template<typename Container, typename Index>
decltype(auto) authAndAccess(Container& c, Index i);

注意到Container是一個非const的左值引用,這意味著用戶可以修改Container內(nèi)元素的值,同時也意味不能傳遞右值引用給它。

另外右值容器一般是一個臨時對象,會在函數(shù)調(diào)用結(jié)束后不久被銷毀,所以當(dāng)用戶傳入一個右值引用的時候,一般我們要把返回元素拷貝它的一個副本。如何能夠在不重載authAndAccess函數(shù)的情況下使它同時支持左值和右值呢?答案是通用引用。

template<typename Container, typename Index>
decltype(auto) authAndAccess(Container&& c,Index i);

為了保證推導(dǎo)結(jié)果的正確性,需要在實現(xiàn)中增加完美轉(zhuǎn)發(fā)(std::forward)功能。

template<typename Container, typename Index>
decltype(auto)authAndAccess(Container&& c, Index i){

 authenticateUser();

 return std::forward<Container>(c)[i];
} // c++ 14版本
template<typename Container, typename Index>
auto authAndAccess(Container&& c, Index i)
-> decltype(std::forward<Container>(c)[i])
{
 authenticateUser();

 return std::forward<Container>(c)[i];
} // c++ 11版本

3. decltype使用的極端case

decltype(auto) f1() { // decltype(x) is int, so f1 returns int
 int x = 0;
 ...
 return x;
}

decltype(auto) f2() { // decltype((x)) is int&, so f2 returns int&
 int x = 0;
 ...
 return (x);
}

返回了一個局部變量的引用。

4. 需要記住的:

1) decltype總是返回與變量或者表達(dá)式完全相同的類型;

2) 對于類型T的非名稱的左值表達(dá)式,decltype總是返回T&;

以上就是c++ decltype關(guān)鍵字的用法的詳細(xì)內(nèi)容,更多關(guān)于c++ decltype關(guān)鍵字的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

万荣县| 石渠县| 岳普湖县| 彰化市| 滨海县| 黑山县| 井冈山市| 佛冈县| 辉县市| 郎溪县| 庄浪县| 莱州市| 马关县| 凉城县| 威远县| 内丘县| 会理县| 中方县| 彭山县| 河池市| 屯留县| 本溪| 偏关县| 惠水县| 宜州市| 繁昌县| 喀喇| 阜新| 武功县| 桦南县| 惠水县| 宁河县| 万年县| 英吉沙县| 都江堰市| 东乌珠穆沁旗| 奉新县| 武功县| 沁水县| 衡山县| 惠水县|