开关“的控制传递绕过以下的初始化:当调用一个函数 [英] switch "transfer of control bypasses initialization of:" when calling a function
问题描述
当我尝试建立以下开关时,我得到一个控制权转移绕过初始化:错误:
switch (retrycancel)
{
case 4://用户按RETRY
//枚举所有可见窗口,并在windows中存储句柄和标题
std :: vector< MainHandles :: window_data> windows = MainHandles()。enum_windows()。get_results();
break;
case 2:
// code
}
它与我的调用我的枚举函数有关。如果不允许从交换机内调用函数,是否有解决这种问题的解决方法?
6.6.4的C ++标准:
goto语句无条件地
将控制转移到语句
标识符。
标识符应为位于当前函数中的标签(6.1)
。
C ++标准:
可以转移到
区块,但不会绕过
初始化声明。 A
程序从点
跳转,其中具有自动
存储持续时间的局部变量不在范围内到
点,其中它在范围内是
ill-除非变量具有POD
类型(3.9)并且未声明
初始化程序
重点。因为切换
真的是 goto
伪装,你会遇到这种行为。要解决这个问题,如果必须使用开关
(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 {
...
}
虽然我不知道你希望通过创建 I get a "transfer of control bypasses initialization of:" error when i try to build the following switch: 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? 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 or refactor into Though it's not obvious to me what you're hoping to accomplish with creating the 这篇关于开关“的控制传递绕过以下的初始化:当调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! windows
开关
里面,因此你可能想重新思考你的设计。 注意我向 Windows
添加了 const
限定符,因为它在您的示例中未修改。 / p> 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
}
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
}
if
/else
if (retrycancel == 4) {
const std::vector<MainHandles::window_data> windows(
MainHandles().enum_windows().get_results()
);
} else if (retrycancel == 2)
// code
} else {
...
}
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.