2011-01-31 81 views
0

我寫了一個類似列表的模板類sll(單鏈表)。現在,我正在嘗試將分配器插入它。我有默認的分配器,分配器和一個基於池的分配器,pool_allocator。這些是在STL分配器接口之後設計的,但我需要將分配器處理的元素數量(max_size)作爲模板參數添加。所以,我做了以下。默認模板模板參數的語法

enum {Default_1 = 16};   // for example 
template <typename T, size_t N = Default_1> 
struct allocator { 
}; 

enum {Default_2 = 32};   // for example 
template <typename T, size_t N = Default_2> 
struct pool_allocator { 
}; 

我想支持兩種用法,如果由客戶端:

1. sll<int> == implying ==> sll<int, allocator<int, Default_1> > 

2. sll<int, pool_allocator<int, 4096> > 

我有被指定在SLL模板類的默認分配的困難。最初我有

template<typename T, typename Allocator = allocator<T> > class sll { ...}; 

它的工作,但問題是,用戶可以;噸指定的分配器的能力。

我試圖

template<typename T, 
    typename Allocator = allocator< typename T, size_t N = Default_3> > 
class sll { ... }; 

,但我收到的錯誤:

error: template argument 1 is invalid 

我嘗試一些其他的組合,但沒有一次成功。此時,我出於想法,尋求SO社區的幫助。任何建議或指針表示讚賞。

回答

3

你必須寫:

template<typename T, 
    typename Allocator = allocator<T, Default_3> > 
class sll { ... }; 
+0

+1:謝謝。這很簡單,我忽略了最簡單的選擇:-(。後續問題是:在sll類模板中,如何訪問分配器的第二個模板參數(注意大寫字母A)? – Arun 2011-02-01 02:11:10