类似于枚举的 using 声明?

发布于 2024-09-28 05:58:47 字数 679 浏览 0 评论 0原文

基本上这就是我想做的:

struct A {
    enum E {
        X, Y, Z
    };
};

template <class T>
struct B {
    using T::E;
};

// basically "import" the A::E enum into B.
std::cout << B<A>::X << std::endl;

原因是我想基本上将实现细节注入到我的模板类中。同时,“模型”的枚举反映了我希望用户能够获得模板的特定实例化的信息。这可能吗?

我知道我可以让 B 继承自 A,但我认为这不是一个理想的解决方案,因为我希望能够添加新的“模型”而不更改B 的胆量。

编辑:现在我已经考虑过了,继承并不一定需要被排除。也许以下是理想的:

struct A {
    enum E {
        X, Y, Z
    };
};

template <class T>
struct B : A {
};

int main() {
    std::cout << B<A>::X << std::endl;
}

Basically this is what I'd like to do:

struct A {
    enum E {
        X, Y, Z
    };
};

template <class T>
struct B {
    using T::E;
};

// basically "import" the A::E enum into B.
std::cout << B<A>::X << std::endl;

The reason why is that I want to basically inject the implementation details into my template class. At the same time, the enum of the "model" reflects information that I want the user to be able to have for a particular instantiation of a template. Is this possible?

I know that I could have B inherit from A, but I think that isn't an ideal solution because I want to be able to add new "models" without changing the guts of B.

EDIT: Now that I've though about it, inheritance doesn't necessarily need to be ruled out. Perhaps the following is ideal:

struct A {
    enum E {
        X, Y, Z
    };
};

template <class T>
struct B : A {
};

int main() {
    std::cout << B<A>::X << std::endl;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木槿暧夏七纪年 2024-10-05 05:58:47

这对我有用:

struct A {
    enum E {
        X, Y, Z
    };
};

template <class T>
struct B {
    typedef typename T::E E;
};

// basically "import" the A::E enum into B.
int main(void)
{
    std::cout << B<A>::E::X << std::endl;
    return 0;
}

输出是

0

我确实收到有关该限定名称中的非标准扩展的警告,因此也许有一个更优雅的解决方案。

This works for me:

struct A {
    enum E {
        X, Y, Z
    };
};

template <class T>
struct B {
    typedef typename T::E E;
};

// basically "import" the A::E enum into B.
int main(void)
{
    std::cout << B<A>::E::X << std::endl;
    return 0;
}

Output is

0

I do get a warning about non-standard extension in that qualified name so perhaps there is a more elegant solution.

才能让你更想念 2024-10-05 05:58:47

我想你可以做

template <class T>
struct B {
    typedef typename T::E E;
};

I think you can do

template <class T>
struct B {
    typedef typename T::E E;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文