I want to specialize a method of a class X
for floating point types.
The following code compiles and works perfectly:
x.hpp:
template <typename T>
class X {
public:
...
T bucket_width(const BucketID index) const;
T bucket_min(const BucketID index) const;
T bucket_max(const BucketID index) const
...
};
x.cpp:
...
template <typename T>
T X<T>::bucket_width(const BucketID index) const {
return bucket_max(index) - bucket_min(index) + 1;
};
template <>
float X<float>::bucket_width(const BucketID index) const {
return bucket_max(index) - bucket_min(index);
};
template <>
double X<double>::bucket_width(const BucketID index) const {
return bucket_max(index) - bucket_min(index);
};
...
Now, similarly to this answer I changed the cpp file to:
template <typename T>
T X<T>::bucket_width(const BucketID index) const {
return bucket_max(index) - bucket_min(index) + 1;
};
template <typename T>
std::enable_if_t<std::is_floating_point_v<T>, T> X<T>::bucket_width(const BucketID index) const {
return bucket_max(index) - bucket_min(index);
};
Unfortunately, this results in the following compiler error:
.../x.cpp:46:56: error: return type of out-of-line definition of 'X::bucket_width' differs from that in the declaration
std::enable_if_t<std::is_floating_point_v<T>, T> X<T>::bucket_width(const BucketID index) const {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
Can someone explain to me what I am missing?
Thanks in advance!
Edit: We explicitly instantiate the template classes at the end of the cpp file so that we can do template code in the cpp file.