为什么这是部分专业化? (我能做什么?)
Why is this partial specialization? (And what can I do?)
我有几个模板的成员函数class,它们本身就是模板。对于所有这些,编译器都会抱怨: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;
}
我有几个模板的成员函数class,它们本身就是模板。对于所有这些,编译器都会抱怨: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;
}