开关“控制权的转移绕过初始化:”当调用函数时

发布于 2024-10-19 06:02:26 字数 408 浏览 1 评论 0原文

当我尝试构建以下开关时,出现“控制转移绕过初始化:”错误:

switch (retrycancel)
{
    case 4:    //The user pressed RETRY
        //Enumerate all visible windows and store handle and caption in "windows"
        std::vector<MainHandles::window_data> windows = MainHandles().enum_windows().get_results(); 
        break;

    case 2: 
        //code
}

它与我调用枚举函数有关。如果不允许从开关内调用函数,是否有解决此类问题的方法?

I get a "transfer of control bypasses initialization of:" error when i try to build the following switch:

switch (retrycancel)
{
    case 4:    //The user pressed RETRY
        //Enumerate all visible windows and store handle and caption in "windows"
        std::vector<MainHandles::window_data> windows = MainHandles().enum_windows().get_results(); 
        break;

    case 2: 
        //code
}

It has something to do with my calling my enumerate function. If it is not allowed to call a function from within a switch, is there a workaround for this kind of problem?

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

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

发布评论

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

评论(2

听闻余生 2024-10-26 06:02:26

C++ 标准第 6.6.4 节:

无条件goto语句
将控制权转移到语句
由标识符标记。这
标识符应为标签 (6.1)
位于当前函数中。

C++ 标准第 6.7 节:

可以转入
阻止,但不是以绕过的方式
带有初始化的声明
。一个
从一点跳转的程序
其中带有自动功能的局部变量
存储持续时间不在范围内
它在范围内的点是
除非变量具有 POD,否则格式错误
类型 (3.9) 并且声明时没有
初始化器

我添加的 强调。由于 switch 实际上是变相的 goto,因此您会遇到这种行为。为了解决这个问题,如果您必须使用 switch

switch (retrycancel)
    {
    case 4:
    {
        const std::vector<MainHandles::window_data> windows(
            MainHandles().enum_windows().get_results()
        );
        break;
    }
    case 2: 
        //code
    }

或重构为 if/else

if (retrycancel == 4) {
    const std::vector<MainHandles::window_data> windows(
        MainHandles().enum_windows().get_results()
    );
} else if (retrycancel == 2)
    // code
} else {
    ...
}

请添加大括号虽然对我来说您所希望的并不明显来完成在 switch 内创建 windows vector,因此您可能需要重新考虑您的设计。 注意 我向 windows 添加了一个 const 限定符,因为它在您的示例中未进行修改。

section 6.6.4 of the C++ standard:

The goto statement unconditionally
transfers control to the statement
labeled by the identifier. The
identifier shall be a label (6.1)
located in the current function.

section 6.7 of the C++ standard:

It is possible to transfer into a
block, but not in a way that bypasses
declarations with initialization
. A
program that jumps from a point
where a local variable with automatic
storage duration is not in scope to a
point where it is in scope is
ill-formed unless the variable has POD
type (3.9) and is declared without an
initializer

Emphasis added by me. Since switch is really goto in disguise, you're encountering this behavior. To solve this, add braces if you must use a switch

switch (retrycancel)
    {
    case 4:
    {
        const std::vector<MainHandles::window_data> windows(
            MainHandles().enum_windows().get_results()
        );
        break;
    }
    case 2: 
        //code
    }

or refactor into if/else

if (retrycancel == 4) {
    const std::vector<MainHandles::window_data> windows(
        MainHandles().enum_windows().get_results()
    );
} else if (retrycancel == 2)
    // code
} else {
    ...
}

Though it's not obvious to me what you're hoping to accomplish with creating the windows vector inside a switch, so you may want to rethink your design. Note I added a const qualifier to windows since it's not modified in your example.

暖树树初阳… 2024-10-26 06:02:26

开关本质上是一个goto,也就是说,它是一个goto到适当的标签。 C++ 标准禁止 goto 绕过非 POD 对象的初始化。将向量声明放入大括号中即可解决问题

switch (retrycancel)
    {
     case 4:                //The user pressed RETRY
     {
        std::vector<MainHandles::window_data> windows = MainHandles().enum_windows().get_results(); //Enumerate all visible windows and store handle and caption in "windows"
        break;
     }
    case 2: 
        //code
    }

a switch is essentially a goto, that is, it is a goto to the appropriate label. The C++ standard forbids a goto to bypass an initialization of a non-POD object. Take the vector declaration into braces and it will solve the problem

switch (retrycancel)
    {
     case 4:                //The user pressed RETRY
     {
        std::vector<MainHandles::window_data> windows = MainHandles().enum_windows().get_results(); //Enumerate all visible windows and store handle and caption in "windows"
        break;
     }
    case 2: 
        //code
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文