c++ decltype關(guān)鍵字的用法
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)文章
C++ 如何判斷四個(gè)點(diǎn)是否構(gòu)成正方形
這篇文章主要介紹了C++ 如何判斷四個(gè)點(diǎn)是否構(gòu)成正方形的案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
C語言實(shí)現(xiàn)C++繼承和多態(tài)的代碼分享
本文主要給大家簡單講訴了C和C++的區(qū)別以及如何使用C語言模擬實(shí)現(xiàn)C++繼承和多態(tài),并附上示例代碼,是篇相當(dāng)不錯(cuò)的文章,推薦給喜歡C語言的小伙伴們2017-07-07
C++中臨時(shí)對象的常見產(chǎn)生情況及其解決的方案
這篇文章主要是探討常見的臨時(shí)對象產(chǎn)生的情況,及其如何避免和解決這種臨時(shí)對象產(chǎn)生的方式。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
C++中一維數(shù)組與指針的關(guān)系詳細(xì)總結(jié)
以下是對C++中一維數(shù)組與指針的關(guān)系進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下2013-09-09

