mercredi 22 septembre 2021

Use alias template from base class in derived class [duplicate]

I have a (probably syntactic) problem when declaring an alias template based on an alias template in a base class. I found 3 working solutions, but the 4th one doesn't work, it's the one I would prefer, but it doesn't compile...

I don't really know what the problem is. Furthermore, I compiled with gcc 4.8.3 and std=c++11 and I got this:

g++ -Wall -pedantic -std=c++11 main.cpp -o out
main.cpp:43:31: error: expected type-specifier
         using ContainerType = BaseClass::ContainerType<T>;
                               ^
main.cpp:46:9: error: ‘ContainerType’ does not name a type
         ContainerType<double> double_container;
     

Please have a look at the code below, ideas and comments are welcome:

template <typename T>
class DummyAllocator
{
public:
    static T dummy_allocate() { return (T)0; }
};

template <typename T, typename _Allocator = DummyAllocator<T> >
class DummyContainer 
{
public:
    DummyContainer() { _Allocator::dummy_allocate(); }
};

template <typename _Allocator>
class Base {
public:
    template <typename T>
    using ContainerType = DummyContainer<T, _Allocator>;

private:
    ContainerType<int> int_container;
};

template <typename _Allocator>
class Derived : public Base<_Allocator>
{
public:
    // (1) This works!
    //template <typename T>
    //using ContainerType = DummyContainer<T, _Allocator>;

    // (2) This works!
    //template <typename T>
    //using ContainerType = Base<_Allocator>::ContainerType<T>;

    // (3) This works!
    //typedef _Allocator Allocator;
    //template <typename T>
    //using ContainerType = Base<Allocator>::ContainerType<T>;

    // (4) This one doesn't compile!
    using BaseClass = Base<_Allocator>;
    template <typename T>
    using ContainerType = BaseClass::ContainerType<T>;

private:
    ContainerType<double> double_container;
};

int main(int, const char**) 
{
    Base<DummyAllocator<int> >    base;
    Derived<DummyAllocator<int> > derived;

    return 0;
}

DummyAllocator serves as an Allocator for DummyContainer, both classes - Base and Derived - have an instance of DummyContainer, templated with int resp. double.

The alias templates take the allocator into account for easier use in a more complex implementation context.

Aucun commentaire:

Enregistrer un commentaire