Michael
Michael

Reputation: 7839

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

I have several member functions of a template class which are templates themselves. For all of them, the compiler complains: error: function template partial specialization is not allowed.

But I do not see why this should be a partial specialization. What can I do to achieve what I wrote in the code below?

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

Upvotes: 0

Views: 160

Answers (1)

TartanLlama
TartanLlama

Reputation: 65770

Your problem is that, as your compiler says, you are trying to partially specialize a function template:

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

You don't need to specialize the function here, just define it normally:

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

Upvotes: 1

Related Questions