警告控制到达非 void 函数 c++ 的末尾

发布于 2024-11-19 02:45:32 字数 521 浏览 3 评论 0原文

我在 C++ 代码中有以下函数(这只是一个示例)

Object& XYZ::getObject(InObj obj) {
  try{ 
      return obj.getObj();
  }
  DC_THROW_ERROR(ExceptionObj, "Object Not Found");   // Macro which throws an exception
}

当我编译上面的代码时,我收到警告(即控制到达非 void 函数的末尾)。宏在编译之前被扩展。因此编译器知道,如果它没有返回某些内容,则会抛出异常。如果是这样为什么编译器会发出警告?这些类型的函数在我的项目中随处可见。为了摆脱这些警告,我在宏后面的行下面写了。

return *(static_cast<Object*>(0)); 

这是正确的修复方法吗?我知道它有点狡猾..我无法更改代码,因为我必须更改大约 1000 个函数。那么谁能告诉我是否有更好的方法来修复它?

谢谢

I have following function in C++ code (Its just an example)

Object& XYZ::getObject(InObj obj) {
  try{ 
      return obj.getObj();
  }
  DC_THROW_ERROR(ExceptionObj, "Object Not Found");   // Macro which throws an exception
}

When I compile the above code I get Warning (ie control reaches end of non-void function)..The Macro gets expanded before compilation. So the compiler is aware that if its not returning something its throwing an exception. If so why does compiler gives warning?? These type of functions are evrywhere in my project. To get rid of these warnings i have written below line after the Macro.

return *(static_cast<Object*>(0)); 

Is it a correct way of fixing it?? I know its bit dodgy.. I can't change the code as for that i have to change around 1000 functions. So can anyone please tell me if there is any better way of fixing it??

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

何必那么矫情 2024-11-26 02:45:32

听编译器的。摆脱(迄今为止未公开的)宏。或者只是修复它,但最好摆脱它——你陷入了 make-C++-look-like-language-X 的事情,这是不好的。

干杯&呵呵,

Listen to the compiler. Get rid of the (so far undisclosed) macro. Or just fix it, but better get rid of it -- you're into the make-C++-look-like-language-X thing, which is ungood.

Cheers & hth.,

2024-11-26 02:45:32

假设问题中的代码忠实地表示了导致问题的代码,则宏 DC_THROW_ERROR 必须类似于

#define DC_THROW_ERROR(err) catch(...) {throw err;}

DC_THROW_ERROR("Object Not Found") 之后的分号,从而导致

Object& XYZ::getObject(InObj obj) {
  try { 
    return obj.getObj();
  }
  // This is the expansion of DC_THROW_ERROR("Object Not Found")
  catch (...) {
    throw ("Object Not Found");
  }

  ; // This is the semicolon that follows DC_THROW_ERROR("Object Not Found")

}

它是那个无关的分号,而不是宏,让编译器对控制到达非 void 函数的末尾感到兴奋。也就是说,由于 C 和 C++ 中的语句应该以分号结尾,因此代码的人类作者很自然地会在宏调用结束后添加分号。

你真的需要一个宏吗?一般来说,宏是邪恶的。这个宏是邪恶的化身。

  • 它使 try-catch 块看起来像无效语法。使代码看起来无效的宏是双重邪恶的。
  • 它重命名语法。执行 #define BEGIN { 是邪恶的。重命名语法的宏是双重邪恶的。
  • 它邀请程序员在末尾添加分号。后面不应该跟分号的宏是双重邪恶的。
  • 宏调用后面不应跟分号显然没有记录。做坏事但不记录邪恶的宏是双重邪恶的。

总而言之,这个宏是 2x2x2x2,或者十六倍邪恶。这就是这个魔鬼代码。

Assuming the code in the question is a faithful representation of the code that is causing the problem, the macro DC_THROW_ERROR must be something like

#define DC_THROW_ERROR(err) catch(...) {throw err;}

That semicolon after the DC_THROW_ERROR("Object Not Found") thus results in

Object& XYZ::getObject(InObj obj) {
  try { 
    return obj.getObj();
  }
  // This is the expansion of DC_THROW_ERROR("Object Not Found")
  catch (...) {
    throw ("Object Not Found");
  }

  ; // This is the semicolon that follows DC_THROW_ERROR("Object Not Found")

}

It is that extraneous semicolon, not the macro, that is getting the compiler in a tizzy about control reaching the end of a non-void function. That said, since statements in C and C++ are supposed to end with a semicolon, it is only natural that the human author of the code will add a semicolon after the end of the macro invocation.

Do you really need a macro here? Macros are evil in general. This macro is evil incarnate.

  • It makes that try-catch block look like invalid syntax. Macros that make the code look invalid are doubly evil.
  • It renames syntax. Doing #define BEGIN { is evil. Macros that rename syntax are doubly evil.
  • It invites the programmer to add that semicolon at the end. Macros that should not be followed with a semicolon are doubly evil.
  • That the macro invocation should not be followed with a semicolon apparently is not documented. Macros that do something evil but don't document the evilness are doubly evil.

All told, this macro is 2x2x2x2, or sixteen-fold evil. That makes this devil code.

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