Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does VC++ introduce the nonstandard keyword: __leave?

According to MSDN:

The __leave statement is more efficient because it does not cause stack unwinding.

To my understanding, that is to say: "Such a common code snippet is dangerous!"

struct A
{
    int* p;

    A() : p(new int) {}
    ~A() { delete p; }
};

void f()
{
    __try
    {
        A a;
        ... // Doing somthing
        if (the thing has gone bad) __leave;
        ... // Continue
    }
    __finally
    {}
}

Is it a best practice to totally avoid __leave in C++ projects?

like image 430
xmllmx Avatar asked Feb 16 '23 06:02

xmllmx


1 Answers

http://msdn.microsoft.com/en-us/library/yb3kz605.aspx

says:

For C++ programs, C++ exception handling should be used instead of structured exception handling. For more information, see Exception Handling in the C++ Language Reference.

Edit: Also, it's usually good to refrain from compiler specific features.

like image 119
Cramer Avatar answered Feb 20 '23 09:02

Cramer