编译器未标记 HRESULT 的错误返回值

发布于 2024-07-25 05:34:58 字数 1024 浏览 9 评论 0原文

我只是花了方式太长时间试图诊断为什么在下面的代码片段中,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 技术交流群。

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

发布评论

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

评论(4

猫烠⑼条掵仅有一顆心 2024-08-01 05:34:58

您正在遭受逗号运算符的困扰,它计算并丢弃其左侧操作数的值,然后将其右侧操作数计算为表达式的值。

此外,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.

陌上芳菲 2024-08-01 05:34:58

你写的是什么:

return (m_pPool->GetFsm()->ProcessEvent(anEvent), false);

它的意思(大致):

bool temp = false;
m_pPool->GetFsm()->ProcessEvent(anEvent);
return temp;

What you wrote:

return (m_pPool->GetFsm()->ProcessEvent(anEvent), false);

What it means (roughly):

bool temp = false;
m_pPool->GetFsm()->ProcessEvent(anEvent);
return temp;
红焚 2024-08-01 05:34:58

逗号实际上是一个有效的运算符 - 请参阅这篇文章

The comma is actually a valid operator - see this post.

半寸时光 2024-08-01 05:34:58

您看到的问题在于两件事。

  1. 逗号运算符被丢弃
    左侧的值和
    仅返回右侧值
    (fasle)

  2. HRESULT 只是一个长值,因此有一个
    值的隐式转换
    HRESULT 为 false,因此没有编译器
    错误。

The problem you are seeing lies in two things.

  1. The comma operator is throwing away
    the value on the left side and
    returning only the right side value
    (fasle)

  2. An HRESULT is just a long value, and therefore there is an
    implicit conversion of the value
    false to HRESULT, thus no compiler
    error.

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