c++ typedef 另一个类的枚举?
所以这是我的问题:
struct A
{
enum A_enum
{
E0,
E1,
E2
};
};
struct B
{
typedef A::A_enum B_enum;
bool test(B_enum val)
{
return (val == E1); // error: "E1" undeclared identifier
}
};
我特别不想说 A::E1
。 如果我尝试 B_enum::E1
我会收到一条警告,指出它是非标准的。 有没有好的方法来做这样的事情?
So here's my problem:
struct A
{
enum A_enum
{
E0,
E1,
E2
};
};
struct B
{
typedef A::A_enum B_enum;
bool test(B_enum val)
{
return (val == E1); // error: "E1" undeclared identifier
}
};
I specifically do not want to say A::E1
. If I try B_enum::E1
I receive a warning that it is nonstandard. Is there a good way to do something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为 A 应该是一个命名空间而不是一个结构体。
I reckon that A should be a namespace instead of a struct.
将枚举放在全局范围内太过暴露,将它们放在类中可能会引入不需要的依赖关系。 对于与类没有紧密链接的枚举,这就是我使用的:
然后您可以在全局范围内使用:
这或多或少模拟了处理枚举范围的 C# 方式。 预处理器将生成以下代码:
然后将给定的枚举引用为
希望有更好的方法来执行此操作,但到目前为止,这是我找到的唯一解决方案。
Putting enum in global scope is too exposed, putting them in a class can introduced undesired dependency. For enum not tightly link to a class, this is what I use:
Then you can use, at global scope:
That is more or less emulating C# way of handling enums scope. The preprocessor will produce this code:
Then a given enum is referenced as
Hopefully there's a better way of doing this but so far, that's the only solution I found.
我过去也遇到过同样的问题,这就是我解决它的方法。
我希望能够在编译时切换库的实现。
其中一个库使用了如下代码:
在我的代码中,我想从用户体验中隐藏库实现(因此我的代码的用户永远不应该看到我在内部使用的库):
解决方案 1:
解决方案 2:
它们是上面代码片段的两个问题。 第一个问题是你必须复制并复制它。 将枚举粘贴到您的命名空间中(但在查找和替换中使用简单的正则表达式,就完成了)。
第二个问题是您的用户必须使用“as”方法,这意味着它并不简单,或者您必须使用第二个解决方案包装方法/函数。
不管怎样,由于不可能跨命名空间注入枚举,所以这个解决方案是你能做的最好的解决方案。 请注意,您的代码用户甚至不知道您在示例代码中使用 Lib1 库。
I've had the same issue in the past, and this is how I've fixed it.
I wanted to be able to switch the implementation of a library at compile time.
One of the lib was using code like this:
In my code, I wanted to hide the library implementation from the user experience (so a user of my code should never see what lib I'm using internally):
Solution 1:
Solution 2:
They are 2 issues with the code fragment above. The first issue is that you must copy & paste the enum inside your namespace, (but with a simple regular expression in find&replace, you're done).
The second issue is that your user will have to use the "as" method, which means that it's not straightforward, or you have to wrap the method/function using the second solution.
Anyway, as it's not possible to inject an enum across namespace, this solution is the best you can do. Notice that your code user don't even know you're using the Lib1 library in the example code.
我遇到了同样的问题,我使用了这个解决方案,而不是使用多余的命名空间。 它将枚举安全地隐藏在类及其显式用户中。
I had the same problem, and I used this solution instead of screwing around with superfluous namespaces. It keeps the enum safely hidden inside the class and its explicit users.
看来没有一个简单的解决方案。 也困扰我。
如果您被允许更改 A,并且不需要将枚举引入到 A 之外的范围,则有一个不依赖于命名空间的解决方案。 与使用命名空间相反,A 和 A_Enum 都可以是嵌套类。
EDIT2:再次删除了第二个解决方案,并不像我想象的那样工作。
It appears there isn't a simple solution. Bothers me too.
If you are allowed to change A there is a solution that doesn't depend on namespaces if inctoduction of the enum to a scope outside A is not desired. In contrast to using namespaces both A and A_Enum can be nested classes.
EDIT2: removed second solution again, doesn't work as I thought.
为什么你在 struct B 中有测试方法? 我认为这没有任何意义。
通过定义结构体 A 并在其中定义一个枚举,您或多或少是在说“这是结构体 A 范围内的一个枚举”,这与说“如果您想使用这个枚举,您必须引用结构A”。
将枚举放在名称空间内并不能解决您的问题。 因为你会遇到同样的范围问题(C4482),
我认为你让事情变得太复杂了。 你觉得这怎么样?
请注意,A::test() 是静态的。 应该是因为您没有对结构状态进行操作。 由于它是静态的,因此您不需要 A 的实例来调用该方法。
Why do you even have the test method in struct B? I don't think it makes any sense.
By defining struct A and defining an enum inside it, you are more or less saying that "here's an enum in scope of struct A" and this is the same as saying "if you want to use this enum, you have to refer to the struct A".
Putting the enum inside a namespace will not solve your problem. Because you will have the same scope problem (C4482)
I think you are making things too complicated. What do you think of this?
Note that A::test() is static. It should be because you aren't operating on the struct state. And since it's static, you dont need an instance of A to call the method.