Using Inner Class with CRTP

Is it possible to use an inner class or enumeration with CRTP? Example.

template<typename Container>
struct ContainerBase
{
    std::map<typename Container::Enum, int> _;
};
struct ConcreteContainer : ContainerBase<ConcreteContainer>
{
    enum class Enum
    {
        left,
        right
    };
};
+4
source share
1 answer

No. Inside the class template, the derived class is not yet fully defined (this is not a complete object in the standard standard).
Actually (working draft):

A class is considered a fully defined object type (or full type) when closed }

Therefore, you cannot expect to be able to access one of the members or what you declared in the derived class from the class template.

, enum , enum - ( ? ? ...). , . . , , .
:

#include <map>

template<typename> struct traits;

template<typename Container>
struct ContainerBase
{
    std::map<typename traits<Container>::Enum, int> _;
};

template<>
struct traits<struct ConcreteContainer> {
    enum class Enum
    {
        left,
        right
    };
};

struct ConcreteContainer : ContainerBase<ConcreteContainer>
{};

int main() {
    ConcreteContainer cc;
    cc._[traits<ConcreteContainer>::Enum::left] = 0;
    cc._[traits<ConcreteContainer>::Enum::right] = 1;
}

wandbox.

+3

Source: https://habr.com/ru/post/1684360/


All Articles