C++ 中的作用域 case 语句:跨作用域 case 标签的目的?

发布于 2024-09-30 11:45:30 字数 505 浏览 5 评论 0原文

我当前的代码中有一个错误,几天来我一直在努力解决这个错误。我将发布下面代码的摘要版本(不是我的实际代码,但它仍然可以编译)。

#include <iostream>

using namespace std;

int main()
{
  int x = 7;

  switch(x)
  {
    case 1:
    {
      case 2:
      cout << "hi";
    }
  }
}

有时我喜欢为我的 switch case 引入更严格的范围,这样局部变量名称就不会干扰我之前使用的变量名称。显然,有一天我接到一个电话或其他事情,并没有按照我的意愿写完案件陈述,但后来确信我已经完成了。情况 2 实际上应该位于嵌套开关中,而不是作为原始外部开关的一部分(对于那些想知道的人,我在代码中使用命名常量,而不仅仅是幻数)。在 g++ 上,在没有任何选项的情况下编译时,我没有收到警告或错误。

我的问题:为什么允许从开关进行跨范围案例跳转?它有什么目的?

I have a bug in my current code that I have been banging my head against for a couple days. I'll post the summary version of the code below (not my actual code, but it still compiles).

#include <iostream>

using namespace std;

int main()
{
  int x = 7;

  switch(x)
  {
    case 1:
    {
      case 2:
      cout << "hi";
    }
  }
}

I like to sometimes introduce a tighter scope to my switch cases so local variable names don't interfere with ones I've used earlier. Apparently I had a phone call to answer or something one day and didn't finish writing the case statement as I wished, but later on was certain that I had. case 2 should have actually been in a nested switch, not as part of the original outter switch (for those wondering, I use named constants in my code, not just magic numbers). On g++, I didn't get a warning or error when compling without any options.

My question: why allow cross-scope case jumps from a switch? What purpose does it serve?

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

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

发布评论

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

评论(5

趁年轻赶紧闹 2024-10-07 11:45:30

达夫的设备很难像已经暗示的那样是跨范围案例标签的“目的”,它只是对其的利用。

事实可能是,它没有有意设计的目的,而仅仅是最简单的可行实现的产物。这种设计使得 switch-case 结构可以准确地执行合理的操作,但也不能明确保护您免受不合理的影响。

Duff's Device is hardly the "purpose" of cross-scope case labels as already suggested, it is merely an exploitation of it.

The truth is probably that it serves no intentionally designed purpose, but is merely an artefact of the most simple possible implementation that works. The design is such that the switch-case construct can do exactly what is reasonable, but does not explicitly protect you from the unreasonable either.

筑梦 2024-10-07 11:45:30

case 标签只是标签,是(编译器生成的)goto 的目的地。

就像普通标签具有函数作用域一样,case 标签也具有 switch 作用域。

唯一合理的优势是 Duff 的设备,但它与现代计算机不太相关。

所以,这是历史性的。

一个“冻结的历史”案例。

干杯&呵呵,

The case labels are just labels, destinations for (compiler-generated) gotos.

In the same way as ordinary labels have function scope, case labels have switch scope.

The only reasonable advantage is Duff's Device, which however is not very relevant on modern computers.

So, it's historical.

A case of "frozen history".

Cheers & hth.,

带上头具痛哭 2024-10-07 11:45:30

switch 语句内的跨范围 case 标签确实有其用途,特别是在实时、嵌入式系统应用程序中。一方面,它们允许更清晰地实现 couroutines。它们还减少或消除了类似低级应用程序中 goto 语句的使用。是的,goto 很丑陋,但是如果您以前编写过设备驱动程序,其中时间和同步问题是常态,那么您会欣赏跨范围 case 标签的有用性,而不是 goto 语句的高维护性。如果你愿意的话,两害相权取其轻。

Cross-scope case labels inside switch statements do serve a purpose, especially in real-time, embedded, system applications. For one thing, they allow cleaner implementations of couroutines. They also reduce or eliminate the use of goto statements in similar low-level applications. Yes, goto's are ugly but if you have written device drivers before where time and sync issues are the norm, you would appreciate the usefulness of cross-scope case labels over the high maintenance of goto statements. The lesser of two evils if you will.

熟人话多 2024-10-07 11:45:30

您上面的代码是以下内容的同义词:

switch (x)
{
     case 1:
     case 2:
     cout << "hi";
}

编译器不会根据 switch-case 功能区分此代码和您发布的代码。它将其解释为指令的多种情况场景。

Your code above is a synonym for:

switch (x)
{
     case 1:
     case 2:
     cout << "hi";
}

The compiler does not differentiate between this code and the code you posted in terms of the switch-case functionality. It's interpreting it as multiple case scenarios for the instructions.

浅忆 2024-10-07 11:45:30

该代码仅因历史原因而编译。

请注意,它可能会导致非常奇怪的未定义行为

// don't try this at home
switch(x) {
  case 1:
  {
    std::string s = "hi!"
    case 2:
    cout << s; // doh!
  }
}

如果x==2,这将访问s而不先调用其构造函数。不过,我希望编译器对此发出警告。

That code compiles for historical reasons only.

Note that it could cause very bizarre versions of Undefined Behavior:

// don't try this at home
switch(x) {
  case 1:
  {
    std::string s = "hi!"
    case 2:
    cout << s; // doh!
  }
}

If x==2, this would access s without calling its constructor first. I'd expect compilers to warn about that, though.

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