专业化本身就是一个模板

Mar*_*ork 10 c++ templates template-specialization template-meta-programming

我有一个模板类,我有一些专业.
但是下一个专业化就是模板本身.你怎么指定这个:

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";}
}
Run Code Online (Sandbox Code Playgroud)

Mat*_* M. 19

琐碎的部分专业化?

template <typename T>
class Action<std::vector<T>> {
public:
  void doStuff() { std::cout << "A Generic Vector\n"; }
};
Run Code Online (Sandbox Code Playgroud)

  • +1.有时(经常?)明显的答案是正确答案:) (2认同)
  • 我猜人们只是喜欢简约:) (2认同)