I have a template function and a partial specialized version of it like this:
template<typename _T>
int values_in_range(const _T &beg, const _T &end) {
throw std::runtime_error("unsupoorted data type");
}
template<>
int values_in_range<int>(const int &beg, const int &end) {
return end - beg;
}
These code works good and then I want to extend the capabilities of the function to a template class that have two template parameters like this:
template<typename _Ty, size_t _Dims>
class vec {
// ...
};
How to partial specialize the function values_in_range
for class vec
with keeping the parameter _Dims
unspecialized?
I try following code but encount error:
template<size_t _Dims>
int values_in_range<vec<int, _Dims>>(const vec<int, _Dims> &beg,
const vec<int, _Dims> &end) { // <-- compiling error
int ret = 0;
for (int i = 0; i < _Dims; ++i) {
//some computing here
}
return ret;
}
The error message:
error: non-class, non-variable partial specialization ‘values_in_range<vec<int, _Dims> >’ is not allowed
[build] 349 | int values_in_range<vec<int, _Dims>>(const vec<int, _Dims> &beg,
[build] | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~