类似于枚举的 using 声明?
基本上这就是我想做的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用:
输出是
我确实收到有关该限定名称中的非标准扩展的警告,因此也许有一个更优雅的解决方案。
This works for me:
Output is
I do get a warning about non-standard extension in that qualified name so perhaps there is a more elegant solution.
我想你可以做
I think you can do