0

While writing template specialized classes, I've got them not seeing anymore a base class member, like in the example below:

#include <iostream>
#include <memory>

using namespace std;

template< typename T >
struct Base
{
    T m_data;
};

template< typename T >
struct A
{
    T data = 0;
};

template< typename T >
struct A< unique_ptr< T > > : Base< unique_ptr< T > >
{
    char to_char()
    {
//        m_data.reset(); // ERROR: m_data was not declared in this scope
        return 'X';
    }
};

template< typename T >
struct A< shared_ptr< T > > : Base< shared_ptr< T > >
{
    int to_int()
    {
//        m_data.reset(); // ERROR: m_data was not declared in this scope
        return 1000;
    }
};

template< typename T >
struct A< shared_ptr< unique_ptr< T > > > : Base< shared_ptr< unique_ptr< T > > >
{
    double to_double() const
    {
//        m_data.reset();  // ERROR: m_data was not declared in this scope
        return 3.14;
    }
};

int main()
{
    A< int > a_0;
    A< shared_ptr< int > > a_1;
    A< unique_ptr< int > > a_2;
    A< shared_ptr< unique_ptr< int > > > a_3;

    cout << a_0.data << endl;
    cout << a_1.to_int() << endl;
    cout << a_2.to_char() << endl;
    cout << a_3.to_double() << endl;

    return 0;
}

I could not give myself any reasonable explanation: the specialization works fine, but Base::m_data is not seen even if everything is defaulted to public by the struct keyword itself. It actually does not seem an access problem from the compiler error, but rather than the member variable itself is no there at all.

nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64

0 Answers0