Re: [問題] template stack
看板C_and_CPP (C/C++)作者littleshan (我要加入劍道社!)時間16年前 (2010/03/22 14:00)推噓2(2推 0噓 1→)留言3則, 3人參與討論串4/4 (看更多)
※ 引述《james732 (好人超)》之銘言:
: 先感謝 littleshan 的回覆
: 我修改之後,還有一點小問題:
: 我必須要明確寫出 allocator<T>,否則無法編譯
: 有沒有辦法讓我不要寫,而是讓 Compiler 自行推導出來呢?
可以節省打字
但自動推導似乎是做不到
因為目前 C++ 語法無從得知某個 template 的預設型別參數
你可以這樣寫看看
template <typename T = int,
template<typename, typename = allocator<T> > class Cont = vector>
class Stack {
public:
T top()
{
typename Cont<T>::iterator iter = cont.end();
return *(--iter);
}
private:
Cont<T> cont;
};
注意 top() 裡面那個 typename 是不能省的
否則 compiler 會把 iterator 當作是 Cont<T> 裡面的一個 member variable
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.29.108
※ 編輯: littleshan 來自: 140.112.29.108 (03/22 14:00)
→
03/22 14:12, , 1F
03/22 14:12, 1F
推
03/22 16:01, , 2F
03/22 16:01, 2F
推
03/22 16:48, , 3F
03/22 16:48, 3F
※ 編輯: littleshan 來自: 118.168.90.159 (03/22 20:17)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 4 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章