我在某个类中有一个模板方法
template<typename T> int get(T &value) const {
...
}
还有几个专业
template<> int AAA::get<int>(int &val) const;
template<> int AAA::get<AAA>(AAA &val) const;
有一个模板类型
template<const int SIZE> class BBB{
...
};
我需要用这种类型专门化我的模板方法。
template<> int AAA::get<BBB<SIZE>>(BBB<SIZE> &val) const;
我知道函数模板部分特化已被禁用。 但也许这个特殊情况有解决方案?
最佳答案
您可以将其变成模板类特化:
class AAA
{
template<typename T> class get_impl
{
public: static int get(T & value) { return(0); }
};
public: template<typename T> int get(T & value) const
{
return(get_impl<T>::get(value));
}
};
template<> class AAA::
get_impl<int>
{
public: static int get(int & value) { return(0); }
};
template<> class AAA::
get_impl<AAA>
{
public: static int get(AAA & value) { return(0); }
};
template<int SIZE> class BBB{};
template<int SIZE> class AAA::
get_impl<BBB<SIZE>>
{
public: static int get(BBB<SIZE> & value) { return(0); }
};
int main()
{
AAA a{};
BBB<5> b{};
a.get(b);
}
关于c++ - 使用模板化类型名的函数模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47670443/