[英]C++11 auto and decltype
在閱讀有關 auto 和 decltype (C++11) 的內容時,我看到以下內容 function
auto findMin(A a, B b) -> decltype(a < b ? a : b)
{
return (a < b) ? a : b;
}
這部分 -> decltype(a < b? a: b) 對我來說很奇怪。 什么樣的 function 聲明或者它只適用於 auto 和 decltype?
這部分 -> decltype(a < b? a: b) 對我來說很奇怪。 什么樣的 function 聲明或僅適用於 auto 和 decltype?
auto
和括號后面的箭頭->
表示尾隨返回類型,它允許您在知道 function 參數后指定返回類型 - 如果您願意,可以更自由。
decltype
為我們提供了聲明的類型,這允許我們檢查表達式的類型。
在三元表達式a < b? a: b
a < b? a: b
,表達式的類型是a
和b
的通用類型。 也就是說,對於int
和double
,常見類型是double
,因為int
提升為double
。
使用提到的表達式decltype(a < b? a: b)
調用decltype
然后給我們一個公共類型,它將是findMin
返回的返回值的類型。 function 仍然需要返回一個值,所以我們在那里也看到了表達式。
Ps 三元表達式的常見類型行為由std::common_type
特征捕獲(自 c++11 起)
decltype()
不同於auto keyword
。 我們在auto
中聲明了一個變量,我們必須給它一個第一個值。 decltype()
對應一個類型。 decltype()
有兩套獨立的規則。
1)decltype(名稱)
例子;
decltype(x)
decltype(x.y)
decltype(ptr->a)
/*The compiler will look at what the type for this name is and think that-
the type obtained with decltype is that type.*/
int ival; decltype(ival) x; -----> int x;
double dval; decltype(dval) x; -----> double x;
struct Data{
int mx;
};
Data data; decltype(data.mx) x; -----> int x;
Data *p=&data; decltype(p->mx) x; -----> int x;
注意:我們可以在任何使用類型的地方使用decltype
,因為 decltype 表示類型。
注意: const
在auto keyword
中被忽略,但const
在decltype
中不被忽略。
const int cx=10; decltype(cx) y=10; -----> const int y=10;
注意: reference(&)
在auto keyword
中被忽略,但reference(&)
在decltype
中不被忽略。
int x=10;
int &r=x;
decltype(r) foo(); -----> int& foo();
int x=10;
const int &r=x;
decltype(r) y=x; -----> const int& y;
注意: decltype
中沒有數組到指針的轉換(數組衰減)。
int a[] {1,2,3,4};
decltype(a) b; -----> int b[4];
2) decltype(表達式)
decltype (x) -----> 1. Rule set
decltype ((x)) -----> 2. Rule set
下面的所有示例都將應用第二個規則集,因此它不再是 name 而是expression
。
decltype (10);
decltype (x+5);
decltype (x++);
decltype ((y));
decltype (*ptr);
decltype (a[5]);
注意:在此規則集中,類型推斷取決於表達式是Lvalue
、 Rvalue
還是Xvalue
。 注意:如果表達式是PRValue expression
,則結果類型直接是該表達式的類型。
decltype(10) x; // x is int.
int x=20;
decltype(x+4.5) dval; // dval is double.
注意:如果decltype
運算符的操作數不是名字,如果expression屬於值范疇PRvalue expression
,則decltype
獲得的type T
。如果表達式屬於Lvalue Lvalue expression
值范疇,則decltype獲得的類型為T&
.如果表達式屬於Xvalue expression
值類,則decltype得到的類型為T&&
。
如果表達式是左值表達式,下面給出了它的狀態示例。
int x=20;
decltype(++x) y=x; -----> int & y;
int x=20;
int *ptr=&x;
decltype(*ptr) y=x; -----> int & y;
int a[10] [20] {};
decltype (a[2][5]) ----->int &;
int x=10;
decltype(x) -----> int
decltype((x)) -----> Since the variable x is an Lvalue, the type of the expression is int&.
注意:編譯器不會為作為 decltype 運算符的操作數的表達式生成操作碼
int x=20;
decltype(x++) y=45; -----> y is int.
decltype(++x) y=x; -----> Since ++x is an Lvalue y is int&. x=20 because the compiler does not generate an opcode for the expression that is the operand of the decltype operator
C++11 允許兩種方式聲明函數的返回類型
return_type function_name(arg1_type arg1, arg2_type arg2);
前任。 整數條(整數 x,整數 y);
自動函數名(arg1_type arg1,arg2_type arg2)-> return_type;
前任。 自動 foo(int x, int y) -> int
第一種方式稱為'前綴返回類型'。 第二個,顯然叫做'后綴返回類型'。 “后綴返回類型”不僅僅是另一種指定返回類型的奇特方式。 它在進行泛型編程時非常重要。
讓我解釋一下。
'auto' 類型說明符是 C++11 提供的一項很棒的功能,它允許您定義可以由編譯器推斷其類型的變量。 但是有一個問題! 程序員需要幫助編譯器使用初始化器推斷“自動”變量的類型。 例如,如果
auto x = 100;
編譯器將 x 的類型推斷為“int”或在以下情況下
auto y{'t'};
編譯器將 y 的類型推斷為“字符列表”
當初始化器的類型很簡單並且如上所述已知時,編譯器推斷類型的工作會更簡單。 但這並不總是那么簡單。 有時,自動變量或返回值的數據類型取決於其他變量的類型/計算。 例如,在模板 function 的情況下,需要根據輸入變量的數據類型和/或計算來確定返回類型。
這就是 decltype() 說明符出現的時候。
decltype(expr)
顧名思義,聲明類型說明符推斷提供給它的表達式的類型。
我之前忘記提到的一條經驗法則是,編譯器從左到右解析表達式。
template<typename X, typename Y>
auto mul(X a, Y b) -> decltype(a*b)
在這里,編譯器將 auto 視為類型的占位符,“decltype”負責根據輸入的表達式進行推導。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.