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

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

 更新時(shí)間: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在實(shí)際的開發(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)鍵字可以獨(dú)立用于對函數(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]
}

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

std::deque<int> d;

...

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

因?yàn)楦鶕?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是一個(gè)非const的左值引用,這意味著用戶可以修改Container內(nèi)元素的值,同時(shí)也意味不能傳遞右值引用給它。

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

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

為了保證推導(dǎo)結(jié)果的正確性,需要在實(shí)現(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);
}

返回了一個(gè)局部變量的引用。

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)文章

最新評論

堆龙德庆县| 安福县| 辉县市| 垣曲县| 三穗县| 永仁县| 高阳县| 常德市| 广州市| 甘谷县| 永康市| 连南| 双鸭山市| 电白县| 东丰县| 土默特右旗| 灌阳县| 军事| 遂昌县| 岐山县| 广河县| 临高县| 体育| 肇州县| 章丘市| 邵阳市| 巴东县| 若尔盖县| 互助| 永川市| 平罗县| 平江县| 泾源县| 广水市| 共和县| 越西县| 华容县| 普安县| 香格里拉县| 裕民县| 图木舒克市|