为什么这是部分专业化?(我能做什么?

Why is this partial specialization? (And what can I do?)

本文关键字:什么 专业化 为什么      更新时间:2023-10-16

我有几个模板类的成员函数,它们本身就是模板。对于所有这些,编译器都抱怨:error: function template partial specialization is not allowed .

但我不明白为什么这应该是部分专业化。我该怎么做才能实现我在下面代码中编写的内容?

template <int DIM>
class A : public B
{
    public:
        template <class T>
        T getValue(void* ptr, int posIdx);
        template <class T>
        T getValue(void* ptr, int posIdx, int valIdx);
        template <class T>
        T getValue(void* ptr, Coords& coord, Coords& size, int valIdx);
        template <class T>
        void setValue(void* ptr, int posIdx, T val);
        template <class T>
        void setValue(void* ptr, int posIdx, int valIdx, T val);
        template <class T>
        void setValue(void* ptr, Coords& coord, Coords& size, int valIdx, T val);
};
// example how the functions are implemented:
template <int DIM>
template <class T>
T A<DIM>::getValue<T>(void* ptr, Coords& coord, Coords& size, int valIdx){
  T val = static_cast<T>(some_value); // actually, its more complicated
  return val;
}

你的问题是,正如你的编译器所说,你正在尝试部分专用化一个函数模板:

template <int DIM>
template <class T>
T A<DIM>::getValue<T>(void* ptr, Coords& coord, Coords& size, int valIdx){
//            here^^^
  T val = static_cast<T>(some_value); // actually, its more complicated
  return val;
}

这里不需要专门化函数,只需正常定义即可:

template <int DIM>
template <class T>
T A<DIM>::getValue(void* ptr, Coords& coord, Coords& size, int valIdx){
//     no more <T>^
  T val = static_cast<T>(some_value); // actually, its more complicated
  return val;
}