How to jump out of a C++ code block?

前端 未结 7 810
无人及你
无人及你 2021-01-04 19:22

This is what I would like to do:

{
    ...
    if(condition)
        break;
    ...
}

This works for a loop. I would like something similar

7条回答
  •  花落未央
    2021-01-04 19:54

    In C++11 the best way to achieve this is use a anonymous lambda function and replacing break with return

    [&](){
        ...
        if(condition)
            return;
        ...
     }();
    

    Note that [&] captures all variables by reference, if you don't use any just replace it with []

提交回复
热议问题