编译器未标记 HRESULT 的错误返回值
我只是花了方式太长时间试图诊断为什么在下面的代码片段中,ProcessEvent()
方法似乎忽略了false
我为 aInvokeEventHandler
传入的值:
HRESULT
CEventManager::
Process(Event anEvent)
{
return (m_pPool->GetFsm()->ProcessEvent(anEvent), false);
}
// Definition of ProcessEvent()
HRESULT ProcessEvent(const Event& anEvent, bool aInvokeEventHandler = true);
每当我中断 ProcessEvent()
方法时,aInvokeEventHandler
始终为 true
,无论我是否传入 false
。
一位同事向我指出,false
值应该位于 return
行的内括号内,如下所示:
return m_pPool->GetFsm()->ProcessEvent(anEvent, false); // Corrected code
尽快当我看到这一幕时,我踢了自己一脚。 发现这一点显然变得更加困难,因为原始编码器在 return
行上使用了多余的外括号。
我的问题是,为什么编译器没有为我选择这个?
我的方法返回一个 HRESULT
,但在上面的原始代码中,我显然返回括号中的一组复合值,即:
(HRESULT, bool)
这样的表示法在 C/C++ 标准中是否可以接受,如果可以的话,允许这样做的目的是什么? 或者这是编译器中的错误?
I just spent way too long trying to diagnose why, in the following snippet of code, the ProcessEvent()
method seemed to be ignoring the false
value I passed in for aInvokeEventHandler
:
HRESULT
CEventManager::
Process(Event anEvent)
{
return (m_pPool->GetFsm()->ProcessEvent(anEvent), false);
}
// Definition of ProcessEvent()
HRESULT ProcessEvent(const Event& anEvent, bool aInvokeEventHandler = true);
Whenever I broke in the ProcessEvent()
method, aInvokeEventHandler
would always be true
, regardless of whether I passed in false
.
It took a workmate to point out to me that the false
value should be inside the inner parentheses on the return
line, like so:
return m_pPool->GetFsm()->ProcessEvent(anEvent, false); // Corrected code
As soon as I saw this, I kicked myself. Spotting this was obviously made harder because the original coder used redundant outer parentheses on the return
line.
My question is, why didn't the compiler pick this up for me?
My method is returning a HRESULT
, yet in the original code above, I am clearly returning a composite set of values in parentheses, i.e:
(HRESULT, bool)
Is notation like this acceptable in the C/C++ standards, and if so, what purpose would there be in allowing this? Or is this a bug in the compiler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在遭受逗号运算符的困扰,它计算并丢弃其左侧操作数的值,然后将其右侧操作数计算为表达式的值。
此外,ProcessEvent 参数的默认值也是您的单参数调用可接受的原因。
You are suffering from the comma operator, which evaluates and discards the value of its left-hand operand, and then evaluates its right-hand operand as the value of the expression.
Also, the default value for the argument to ProcessEvent is why your one-argument call was acceptable.
你写的是什么:
它的意思(大致):
What you wrote:
What it means (roughly):
逗号实际上是一个有效的运算符 - 请参阅这篇文章。
The comma is actually a valid operator - see this post.
您看到的问题在于两件事。
逗号运算符被丢弃
左侧的值和
仅返回右侧值
(fasle)
HRESULT 只是一个长值,因此有一个
值的隐式转换
HRESULT 为 false,因此没有编译器
错误。
The problem you are seeing lies in two things.
The comma operator is throwing away
the value on the left side and
returning only the right side value
(fasle)
An HRESULT is just a long value, and therefore there is an
implicit conversion of the value
false to HRESULT, thus no compiler
error.