Re: [問題] auto用法一問

作者: dzwei (Cout<< *p << \n ;)   2021-11-15 03:20:43
首先第一點
auto只能推導 靜態型別
也就是編譯時期就能確定的型別
所以看到程式中有virtual這個關鍵字
用auto就要小心了
但原po這個例子沒問題
再來回到原PO的問題
Q: 你如何知道你的auto推導出來的是什麼型別?
這邊提供一個檢查技巧/範例
(effective modern c++這本書有)
//先創建一個測試用的class TD
template<typename T> // declaration only for TD;
class TD; // TD == "Type Displayer"
//以auto宣告型別並初始化
auto t = "some string";
//使用TD
TD<decltype(t)> tType;
//這時候你通常會看到類似的編譯錯誤訊息
error: aggregate 'TD<char*> tType' has incomplete type and
cannot be defined
那個 'TD<char*> tType'裡面的char*就是你用auto推出來的型別
再看這例子
std::vector<bool> features(const Widget& w);
Widget w;

auto highPriority = features(w)[5]; // is w high priority?

processWidget(w, highPriority); // process w in accord
// with its priority
你猜highPriority會被推成什麼型別? 理想的bool?
不對 它被推成
std::vector<bool>::reference 這個型別
這關係到std::vector<bool>裡面的實作方式
這時候只能避開auto了
乖乖使用bool
但可以用上面的方式先檢查推導出來的型別
(後面一點的compiler 好像可以正常推導出bool)
補充:
個人最推薦的auto使用情境
template<typename It> // algorithm to dwim ("do what I mean")
void dwim(It b, It e) // for all elements in range from
{
//想自動取得It<T> 裡面的型別T作為初始化宣告
T val = ...
}
你有想過我要如何自動得到並初始化
It<T>裡面的型別T嗎?
答案是要用到trait-class
(沒用auto的話)
template<typename It> // algorithm to dwim ("do what I mean")
void dwim(It b, It e) // for all elements in range from
{ // b to e
while (b != e) {
typename
std::iterator_traits<It>::value_type
currValue = *b;

}
}
三小?
這麼簡地的事情
也要弄到那麼複雜的trait-class?
C++11自從有了auto之後,
這件事情不再痛苦
這件事情不再痛苦
template<typename It> // as before
void dwim(It b, It e)
{
while (b != e) {
auto currValue = *b;

}
參考:
Effective Modern C++ item5 與 item6
<大推這本書>
作者: F04E (Fujitsu)   2021-11-16 10:37:00
可惜作者已經...
作者: lc85301 (pomelocandy)   2021-11-16 11:49:00
作者怎麼了owo
作者: F04E (Fujitsu)   2021-11-16 11:57:00
在2015年宣布退出C++世界... QQ
作者: WangDaMing (王大明)   2021-11-16 22:04:00
剛查了~還真退了!本來在等他講c++20 XD
作者: F04E (Fujitsu)   2021-11-17 08:17:00
宣布退出的時候本來還說會對已出版的書持續勘誤但在2018時大師發現自己已經無法對讀者寄來的TR進行確認所以現在連勘誤都...
作者: unmolk (UJ)   2021-11-25 12:22:00
一樓那樣講我還以為QAQ
作者: CoNsTaR ((const *))   2021-11-26 09:40:00
一樓笑死 很故意 XDDD

Links booklink

Contact Us: admin [ a t ] ucptt.com