10

我有一個模板類,我有一些專業化的。
但下一個專業化是模板本身。你如何指定這個:專業化本身就是一個模板

template<typename T> 
class Action 
{ 
    public: void doStuff() { std::cout << "Generic\n"; } 
} 

// A specialization for a person 
template<> 
class Action<Person> 
{ 
    public: void doStuff() { std::cout << "A Person\n";} 
} 


// I can easily specialize for vectors of a particular type. 
// But how do I change the following so that it works with all types of vector. 
// Not just `int` 
template<> 
class Action<std::vector<int> > 
{ 
    public: void doStuff() { std::cout << "A Generic Vector\n";} 
} 
+2

我只是想知道你不知道這個! – Nawaz 2012-01-16 08:16:28

+0

@Nawaz:我的想法,也許是深夜吧? – 2012-01-16 08:24:28

+0

現在我明白了答案,但我一直在其中添加另一個模板行。 – 2012-01-16 18:47:20

回答

19

瑣碎的偏特化?

template <typename T> 
class Action<std::vector<T>> { 
public: 
    void doStuff() { std::cout << "A Generic Vector\n"; } 
}; 
+2

+1。有時(經常?)顯而易見的答案是正確的答案:) – ereOn 2012-01-16 08:02:32

+0

@ereOn:真正的問題是 - >爲什麼我得到如此之多的選票以獲得如此微不足道的答案:x? – 2012-01-16 08:24:54

+2

我想人們只是喜歡簡單:) – ereOn 2012-01-16 08:27:26