do {…} while(false)

前端 未结 25 2442
小鲜肉
小鲜肉 2020-11-28 03:29

I was looking at some code by an individual and noticed he seems to have a pattern in his functions:

 function()
{
 

        
25条回答
  •  -上瘾入骨i
    2020-11-28 03:53

    It's simple: Apparently you can jump out of the fake loop at any time using the break statement. Furthermore, the do block is a separate scope (which could also be achieved with { ... } only).

    In such a situation, it might be a better idea to use RAII (objects automatically destructing correctly when the function ends). Another similar construct is the use of goto - yes, I know it's evil, but it can be used to have common cleanup code like so:

     function()
    {
     
    
     
    error: }

    (As an aside: The do-while-false construct is used in Lua to come up for the missing continue statement.)

提交回复
热议问题