Because partial specialization of a templated member function is not possible in C++ I now try to circumvent this through class template inheritance.
In this approach, I derive from the base class, specifying only the template parameters for which I want to specialize, leaving the remainder as template parameters in the derived class:
template <typename X, typename Y, typename Z>
class Base{};
template <typename Z> : public Base<int, int, Z>
class Derived{};
I wonder whether it is possible to access the specified parameters in the inheritance relation from the inside of the derived class?
I can image that you store those parameters as static constexpr
in Base
, thus making them available in Derived
.
However, is there another way without explicitly storing them yourself as static constexpr
attributes?
Here is an example I wish to improve:
#include <cstdint>
#include <iostream>
template<std::size_t Dimension, std::size_t Size, typename Real>
class BaseDistribution
{
public:
static constexpr std::size_t dim_ = Dimension;
static constexpr std::size_t size_ = Size;
};
template<typename Real>
class DerivedDistribution : public BaseDistribution<2, 9, Real>
{
public:
void print_dimension()
{
std::cout << "Dimension: " << this->dim_ << std::endl;
}
void print_size()
{
std::cout << "Size: " << this->size_ << std::endl;
}
};
int main()
{
DerivedDistribution foo = DerivedDistribution<double>();
foo.print_dimension();
foo.print_size();
return 0;
}