mardi 15 juin 2021

why this typedef is not recognized when the child class is template?

This code compiles perfectly :

# include <iostream>
# include <typeinfo>

template <typename T>
class HasThisClass
{
    protected:
        typedef T ThisClass;
};

class Foo : private HasThisClass<Foo>
{
    public:
        static void printName()
        { std::cout << typeid(ThisClass).name() << std::endl; }
};

int main()
{
    Foo::printName();
    return 0;
}

This code doesn't compile :

# include <iostream>
# include <typeinfo>

template <typename T>
class HasThisClass
{
    protected:
        typedef T ThisClass;
};

template <typename T>
class Bar : private HasThisClass<Bar<T>>
{
    public:
        static void printName()
        { std::cout << typeid(ThisClass).name() << std::endl; } // ERROR here
};

int main()
{
    Bar<double>::printName();
    return 0;
}

with the following error:

error: ‘ThisClass’ was not declared in this scope

I don't understand why it doesn't work when the Bar class is template. How can I obtain the expected behaviour ?

Note 1: I don't want to solve the problem by calling typeid(Bar<double>).name() or typeid(typename HasThisClass<Bar<double>>::ThisClass).name() because in my case the call to ThisClass is written by a macro.

Note 2: If you know another way to manipulate the local class type in a static function, it could solve my problem.

Note 3: I don't use C++14 or >=

Aucun commentaire:

Enregistrer un commentaire