为什么负值枚举会在 Objective-C/C 中引起问题?
由于各种实现原因,我定义了以下枚举:
typedef enum HBSnakeMovementDirection
{
HBSnakeMovementDirectionUp = 1,
HBSnakeMovementDirectionDown = -1,
HBSnakeMovementDirectionRight = 2,
HBSnakeMovementDirectionLeft = -2
}
HBSnakeMovementDirection;
但是,如果我尝试使用 HBSnakeMovementDirectionRight
,我会收到以下警告:
隐式转换更改符号性:'int' 为 'HBSnakeMovementDirection'
它与任何其他枚举值都没有问题。这里有什么问题?我认为这可能与混合负数和正数枚举值有关,但我找不到任何关于此的明确信息。
(我能够想出所有积极的枚举值,让我能够解决这个问题,但它仍然困扰着我,所以我想我会问这个问题。)
我应该声明,就像我的所有项目一样,我启用几乎所有警告(因此,-Wconversion
的抱怨)并将其视为错误。 (我喜欢在编译时尽可能严格。)我使用的是 LLVM 1.6。
更新 1:从字面上看,任何使用 HBSnakeMovementDirectionRight
都会导致前面的警告:
HBSnakeMovementDirection movementDirectionRight = HBSnakeMovementDirectionRight;
我必须将 HBSnakeMovementDirectionRight
转换为 HBSnakeMovementDirection
到使警告静音。
更新2:根据要求,这是在我的机器上发出的完整构建命令:
更新 3:这是我正在 GitHub 上托管的具体项目:
https:// /github.com/LucasTizma/Hebi
具体来说,如下树:
https://github.com/LucasTizma/鹤壁/tree/89262e2e53881584daf029e3dd5f1e99dfbd6f96
For various implementation reasons, I've defined the following enum:
typedef enum HBSnakeMovementDirection
{
HBSnakeMovementDirectionUp = 1,
HBSnakeMovementDirectionDown = -1,
HBSnakeMovementDirectionRight = 2,
HBSnakeMovementDirectionLeft = -2
}
HBSnakeMovementDirection;
However, if I try to use HBSnakeMovementDirectionRight
, I get the following warning:
Implicit conversion changes signedness: 'int' to 'HBSnakeMovementDirection'
It has no problem with any of the other enum values. What's the problem here? I thought it might have to do with mixing negative and positive enum values, but I can't find out anything definitive about this.
(I was able to come up with all positive enum values that allow me to work around this issue, but it still stumped me, so I thought I'd ask about it.)
I should state that, as with all my projects, I enable almost every warning—hence, -Wconversion
's complaints—and treat them as errors. (I like to be as strict as possible at compile time.) I'm using LLVM 1.6.
UPDATE 1: Literally any use of HBSnakeMovementDirectionRight
results in the preceding warning:
HBSnakeMovementDirection movementDirectionRight = HBSnakeMovementDirectionRight;
I have to cast HBSnakeMovementDirectionRight
to HBSnakeMovementDirection
to silence the warning.
UPDATE 2: As requested, here is the entire build command that's being issued on my machine:
UPDATE 3: Here is the exact project I'm working on hosted on GitHub:
https://github.com/LucasTizma/Hebi
Specifically, the following tree:
https://github.com/LucasTizma/Hebi/tree/89262e2e53881584daf029e3dd5f1e99dfbd6f96
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Darren 所说,它看起来确实像一个编译器错误,而 Dave 说 Clang 2.0 不会发生这种情况。
我发现以下类型定义使 OP 代码可以使用 Clang 1.6 进行编译:(
注意附加的
NBSnakeMovementDirectionNone
)这可能与 LLVM 错误 1884,已修复。
As Darren said, it does look like a compiler bug, and Dave said it doesn’t happen with Clang 2.0.
I’ve found that the following type definition makes the OP code compile with Clang 1.6:
(note the additional
NBSnakeMovementDirectionNone
)This could be related to LLVM bug 1884, which has been fixed.
我可以重现这个。对我来说,它确实看起来像是一个编译器错误。枚举中存在负值会导致编译器错误地认为“2”的值超出了枚举的范围,因此出现警告。
无论您指定“2”还是“HBSnakeMovementDirectionRight”,行为都是相同的:它接受 1 并拒绝 2。
编辑:我在现有的 iPhone 项目中对此进行了测试,设置编译器 LLVM 1.6 并设置
-Wconversion
旗帜。I can reproduce this. It certainly looks like a compiler bug to me. The presence of negative values in the enum causes the compiler to mistakenly think that the value of "2" is outside of the range of the enum, hence the warning.
The behavior is the same whether you specify "2" or "HBSnakeMovementDirectionRight": It accepts 1 and rejects 2.
Edit: I tested this in an existing iPhone project, setting the compiler LLVM 1.6 and setting the
-Wconversion
flag.绝对看起来像一个编译器错误。我在 Xcode 3 中打开该项目并编译,并收到错误。当我在 Xcode 4 中打开项目并使用 clang2.0 编译器时,我没有收到任何警告。
Definitely looks like a compiler bug. I opened the project in Xcode 3 and compiled, and got the error. When I opened the project in Xcode 4 and used the clang2.0 compiler, I got no warnings.