作者:
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
<大推這本書>