2011-04-21 79 views
2

我可以這樣做傳遞非特模板作爲模板參數

template<class Key, class Data, class Compare = less<Key>, template<typename T> class Allocator<T> = allocator<T> > 
    class mymap { 
     typedef map<Key,Data,Compare,Allocator<pair<const Key, Data> > > storageMap; 
     typedef vector<Data,Allocator<Data> > storageVector; 
} 

所以模板傳遞給類unspecialiazed後來實例化。

回答

4

是的,這裏有一個最小的可編譯例子:

#include <map> 
#include <vector> 
using namespace std; 

template < 
    class Key, 
    class Data, 
    class Compare = less<Key>, 
    template <typename T> class Allocator = allocator 
> 
class mymap 
{ 
public: 
    typedef map<Key,Data,Compare,Allocator<pair<const Key, Data> > > storageMap; 
    typedef vector<Data,Allocator<Data> > storageVector; 
}; 

int main() 
{ 
    mymap<int,long>::storageMap m; 
    mymap<int,long>::storageVector v; 
    return 0; 
} 
+0

感謝您的協助! – akashihi 2011-04-21 14:39:26

1

是的,這就是所謂的 「模板,模板參數」,並且語法

template <class Key, class Data, class Compare = less<Key>, 
      template <typename T> class Allocator = allocator > 
class mymap { 
    typedef map<Key,Data,Compare,Allocator<pair<const Key, Data> > > storageMap; 
    typedef vector<Data,Allocator<Data> > storageVector; 
} 
+0

'code'mymap.hpp(30) :錯誤:標識符「T」未定義 template ,class Compare = less ,template class Allocator = allocator >'code' @mmutz好,gcc不高興與這樣的聲明:) – akashihi 2011-04-21 14:30:26

+2

@akashi:'s/=分配器/=分配器/ – 2011-04-21 14:32:48

+0

我的錯,抱歉謝謝你的協助 – akashihi 2011-04-21 14:39:07

相關問題