C++自修入門實境秀、C++ Primer 5版研讀秀 10/ ~頁71 decltype 型別指定符-20190718_221144





0:25 type specifier

5:33 2.5.2. The auto Type Specifier

auto型別指定符



10:00 型別type的判斷在程式設計的實務上或許常會是一項困難的挑戰



4:00ImTranstator外掛(擴充功能)的設定

20:00

頁69

type specifier: auto、double、int、char……

specifier翻成「具名、指名」也行。



26:44

Compound Types, const , and auto

The type that the compiler infers for auto is not always exactly the same as the initializer’s type. Instead, the compiler adjusts the type to conform to normal initialization rules.2:21:42

編譯器為auto推導的型別並不一定會跟初始器的型別完全吻合。取而代之相對的甚至是、更誇張的是、反之,編譯器反而會為了去遵循正常的初始化的規則來而調整型別。



1:5:50 auto & const l=i;

這裡的const是沒有意義而被忽略的。l依然可以被覆寫或指定值(assigned)



1:14:40 deduce

2:13:50

Second, auto ordinarily ignores top-level const s (§ 2.4.3 , p. 63 ). As usual in initializations, low-level const s, such as when an initializer is a pointer to const , are kept:2:14:35

其次,auto通常會忽略頂層的const (§ 2.4.3 )。就跟普通初始化一樣,低層的const會被保留,例如初始器是對const的一個指標時:

就跟普通的初始化一樣,低層的const會被保留,舉例來說:若初始器是用了一個對const的指標(即這個指標是指向const──不是指標本身是const喔)時:



const auto &j = 42; // ok: we can bind a const reference to a literal





頁70

練習2.33 1:20:00 1:44:30

練習2.34 1:31:00

int i = 0, &r = i;

auto a = r; // a是一個int (r是i的一個別名,它有型別int)

cout << a << endl;

a = 42;

cout << a << endl;

const int ci = i, & cr = ci;

auto b = ci; // b是一個int ( ci中的頂層const被丟棄了)

b = 42;

auto c = cr; // c是一個int ( cr是ci的一個別名,其const是頂層的)

c = 42;

auto d = &i; // d 是一個 int * (—個 int 物件的 & 是 int*)

auto e = &ci;// e 是 const int* (一個 const 物件的 & 是低層的 const)



auto& g = ci; // g 是一個 const int&,繫結到了 ci

//g = 42;

//auto& h = 42; //錯誤:我們不能將一個普通的參考繫結至一個字面值



const auto& j = 42; // ok:我們可以將一個const參考繫結至一個字面值



練習2.35 1:51:10 1:55:20 2:8:40

const int i = 42;

auto j = i;//j is “int type” auto在判斷的時候,若不是指標或參考,就會忽略top-level const

const auto& k = i; /* &k is "const int &" type;if without the left const ,

k will be int &*/

auto* p = &i;//pointer p is "const int*"

auto & R = i;// R is "const int &"

const auto j2 = i, & k2 = i;

//j2 is "const int",&k2 is "const int &"



2:28:44

2.5.3 decltype型別指定符(type specifier)

字形結構兼音義 ③字形結構兼音義 九陽神功第3招

decl →declare



2:37:30

可見evaluate和analyze不同!

analyze不進行實質的運算,而evaluate會。



2:48:30 subtly 雖微而著,雖小而要



頁71

參考一定要被初始化!

3:0:31

It is worth noting that decltype is the only context in which a variable defined as a reference is not treated as a synonym for the object to which it refers.3:6:50

值得注意的是,decltype是定義為參考的變數不會被當作其所指涉的物件之同義詞的唯一情境。3:9:33

⑦先抓動詞 ⑧找對主詞 is not treated as的主詞是「a variable defined as a reference」(一個被定義為參考的變數)

只有在有「decltype」出現的情境(context上下文)中,3:13:54一個被定義為參考的變數才有機會不必是它所參考對象object的同義詞(別名)。也只有在這個情形下,一個參考才能獨立於它所參考的物件而存在、而被使用。因為對於參考所做的任何運算,其實都是對其所參考的物件的運算,而不是對參考在運算。但decltype出現的地方(情境、情況context),因為對這個參考,只是分析、評估它的type,而並不是進行evaluate運算,所以不涉及參考所參考的對象object本身。3:17:10這時候這個參考才可以不用管它的object而自己存在。

decltype和參考3:5:40

留言

熱門文章