轉自https://blog.csdn.net/yhl_leo/article/details/50865552
1 基本語法
decltype
類型說明符生成指定表達式的類型。在此過程中,編譯器分析表達式並得到它的類型,卻不實際計算表達式的值。
語法為:
decltype( expression )
- 1
編譯器使用下列規則來確定expression
參數的類型。
- 如果
expression
參數是標識符或類成員訪問,則decltype(expression)
是expression
命名的實體的類型。如果不存在此類實體或expression
參數命名一組重載函數,則編譯器將生成錯誤消息。 - 如果
expression
參數是對一個函數或一個重載運算符函數的調用,則decltype(expression)
是函數的返回類型。將忽略重載運算符兩邊的括號。 - 如果
expression
參數是右值,則decltype(expression)
是expression
類型。如果expression
參數是左值,則decltype(expression)
是對 左值引用 類型的expression
。
給出如下示例代碼:
int var; const int&& fx(); struct A { double x; } const A* a = new A();
- 1
- 2
- 3
- 4
語句 | 類型 | 注釋 |
---|---|---|
decltype(fx()); |
const int && |
對左值引用的const int |
decltype(var); |
int |
變量 var 的類型 |
decltype(a->x); |
double |
成員訪問的類型 |
decltype((a->x)); |
const double& |
內部括號導致語句作為表達式而不是成員訪問計算。由於a 聲明為 const 指針,因此類型是對const double 的引用。 |
2 decltype
和引用
如果decltype
使用的表達式不是一個變量,則decltype
返回表達式結果對應的類型。但是有些時候,一些表達式向decltype
返回一個引用類型。一般來說,當這種情形發生時,意味着該表達式的結果對象能作為一條賦值語句的左值:
// decltype的結果可以是引用類型 int i = 42, *p = &i, &r = i; decltype(r + 0) b; // OK, 加法的結果是int,因此b是一個(未初始化)的int decltype(*p) c; // Error, c是int&, 必須初始化
- 1
- 2
- 3
- 4
因為r
是一個引用,因此decltype(r)
的結果是引用類型,如果想讓結果類型是r
所指的類型,可以把r
作為表達式的一部分,如r+0
,顯然這個表達式的結果將是一個具體的值而非一個引用。
另一方面,如果表達式的內容是解引用操作,則decltype
將得到引用類型。正如我們所熟悉的那樣,解引用指針可以得到指針所指對象,而且還能給這個對象賦值,因此,decltype(*p)
的結果類型是int&
而非int
。
3 decltype
和auto
- 處理頂層const和引用的方式不同(參考閱讀:C++ auto類型說明符)
如果decltype
使用的表達式是一個變量,則decltype
返回該變量的類型(包括頂層const
和引用在內):
const int ci = 0, &cj = ci; decltype(ci) x = 0; // x的類型是const int decltype(cj) y = x; // y的類型是const int&,y綁定到變量x decltype(cj) z; // Error, z是一個引用,必須初始化
- 1
- 2
- 3
- 4
decltype
的結果類型與表達式形式密切相關
對於decltype
所用的引用來說,如果變量名加上了一對括號,則得到的類型與不加括號時會有所不同。如果decltype
使用的是一個不加括號的變量,則得到的結果就是該變量的類型;如果給變量加上了一層或多層括號,編譯器就會把它當成是一個表達式。
decltype((i)) d; // Error, d是int&, 必須初始化 decltype(i) e; // OK, e是一個未初始化的int
- 1
- 2
- 模板函數的返回類型
- 在 C++11 中,可以結合使用尾隨返回類型上的
decltype
類型說明符和auto
關鍵字來聲明其返回類型依賴於其模板參數類型的模板函數。 - 在 C++14 中,可以使用不帶尾隨返回類型的
decltype(auto)
來聲明其返回類型取決於其模板參數類型的模板函數。
- 在 C++11 中,可以結合使用尾隨返回類型上的
例如,定義一個求和模板函數:
//C++11 template<typename T, typename U> auto myFunc(T&& t, U&& u) -> decltype (forward<T>(t) + forward<U>(u)) { return forward<T>(t) + forward<U>(u); }; //C++14 template<typename T, typename U> decltype(auto) myFunc(T&& t, U&& u) { return forward<T>(t) + forward<U>(u); };
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
(forward
:如果參數是右值或右值引用,則有條件地將其參數強制轉換為右值引用。)
附上一段源碼:
#include <iostream> #include <string> #include <utility> #include <iomanip> using namespace std; template<typename T1, typename T2> auto Plus(T1&& t1, T2&& t2) -> decltype(forward<T1>(t1) + forward<T2>(t2)) { return forward<T1>(t1) + forward<T2>(t2); } class X { friend X operator+(const X& x1, const X& x2) { return X(x1.m_data + x2.m_data); } public: X(int data) : m_data(data) {} int Dump() const { return m_data;} private: int m_data; }; int main() { // Integer int i = 4; cout << "Plus(i, 9) = " << Plus(i, 9) << endl; // Floating point float dx = 4.0; float dy = 9.5; cout << setprecision(3) << "Plus(dx, dy) = " << Plus(dx, dy) << endl; // String string hello = "Hello, "; string world = "world!"; cout << Plus(hello, world) << endl; // Custom type X x1(20); X x2(22); X x3 = Plus(x1, x2); cout << "x3.Dump() = " << x3.Dump() << endl; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
運行結果為:
Plus(i, 9) = 13 Plus(dx, dy) = 13.5 Hello, world! x3.Dump() = 42
- 1
- 2
- 3
- 4
參考資料:
- Microsoft Developer Network: decltype(C++)
- C++ Primer(第五版)》 Stanley B. Lippman.