C and C++ Should Add This Feature: Self-destructive Pointer

Imagine a function with 10 pointers and 5 locations of return statement, this results in writing 50 lines to delete the pointers. A syntax for self-destructive pointer should be added into C and C++ languages, just add a tilde ~ in front of variable declaration and it is destroyed at any return or at the end of function.

// Example
void func(){
    ~foo*   My_Var1 = new foo();    // Self-destructive
    ~bar*   My_Var2 = new bar();    // Self-destructive
    foobar* My_Var3 = new foobar(); // Not self-destructive
    barfoo* My_Var4 = new barfoo(); // Not self-destructive

    if (){
        do_something();
        // Work-around:
        goto ret;

        // // These can be omitted with self-des pointers
        // delete My_Var1;
        // delete My_var2;
        return;
    }

    if (){
        do_something();
        // Work-around:
        goto ret;

        // // These can be omitted with self-des pointers
        // delete My_Var1;
        // delete My_var2;
        return;
    }
    else{
        do_something();
        // Work-around:
        goto ret;

        // // These can be omitted with self-des pointers
        // delete My_Var1;
        // delete My_var2;
        return;
    }

    ret:
    // // These can be omitted with self-des pointers
    // delete My_Var1;
    // delete My_Var2;
}

You'll only receive email when they publish something new.

More from 19411
All posts