Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data member access in template programming

I am creating a template class with specialized behavior for two different sizes, and general behavior in general class as below::

template<typename T, size_t DIM>
class Dataset
{
public:
    // all the constructors are defaulted
    // all the general behavior implementation

    std::vector<T> _data;
};

Given the flow of data for the class below, I am expecting to have access to _data vector, right ?!

template<typename T>
class Dataset<T, 1>
{
public:
    T & operator()(const size_t & index)
    {
        return _data[index];
    }
};

however, I get the compilation error of _data couldn't be resolved. What is the problem here ?!! Thanks

like image 637
apramc Avatar asked Mar 10 '23 05:03

apramc


1 Answers

A class template specialization is its very own class, unrelated to the primary template. So Dataset<T, 1> does not have a _data member because you did not declare one in its class definition.

If you need common features among different specializations of the same template, you can move them to a shared base class.

like image 82
aschepler Avatar answered Mar 23 '23 12:03

aschepler