Using inner class with CRTP

本小妞迷上赌 提交于 2021-02-04 17:54:05

问题


Is there any possibility to use inner class or enum with CRTP? Ex.

template<typename Container>
struct ContainerBase
{
    std::map<typename Container::Enum, int> _;
};
struct ConcreteContainer : ContainerBase<ConcreteContainer>
{
    enum class Enum
    {
        left,
        right
    };
};

回答1:


No. Within the class template the derived class isn't fully defined yet (that's not a complete object in standardese).
In fact (working draft):

A class is considered a completely-defined object type (or complete type) at the closing }

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

You can work around it by passing the enum separately, but it requires you to define the enum somewhere else (Another base class? The outer scope? Whatever...). You can work around by using traits classes. And so on.
There are several alternatives to do that, but you cannot access directly the enum defined in the derived class.
Here is an example of a viable solution:

#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;
}

See it up and running on wandbox.



来源:https://stackoverflow.com/questions/45859050/using-inner-class-with-crtp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!