gpt4 book ai didi

c++ - 本身就是模板的特化

转载 作者:可可西里 更新时间:2023-11-01 15:41:34 25 4
gpt4 key购买 nike

我有一个模板类,我有一些专长。
但下一个特化是模板本身。你如何指定这个:

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";}
}

最佳答案

平凡的偏特化?

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

关于c++ - 本身就是模板的特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8876835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com