Why is this legal, C ++ typedef func

I did this in msvc 2005.

typedef void (*cleanup_t)();

void func(cleanup_t clean)
{
    cleanup_t();
}

Why is this compiling? and not warn me? Well, that gave me a warning about an irreversible official parameter, but I did it initially when the net was in class. No, there was no indescribable formal parameter when this code gave me problems.

What is cleanup_t (); really doing and what's the point? now for fun I tried int () and it worked too.

+3
source share
2 answers

Runs the default initializer for the cleanup_t type to create a temporary type of this type, and then never uses this temporary.

, "MyClass()" "MyClass c = MyClass();", , . , "MyClass()" , . "MyClass()" "MyClass(). Some_method();" , .

"int()" - "int (0)", "(int) 0", "0". , , , .

-Wall GCC, , " ". , , , , "clean();", , , , . ; -)

+9

, , cleanup_t. , , NULL- , void.

C/++ ( , ) , , :

1 + 2;

, . , NULL , (, assert()).

cleanup_t. ( typedef ) ++, , , , , . , , .

- :

template <class T>
class foo
{
    T myT;

    public:

    foo() {
        myT = T();
    };
};

typedef void (*cleanup_t)();


class bar
{
};


int not_quite_a_cleanup_t_func()
{
    return 1;
}


int main()
{
    foo<int> intFoo;
    foo<cleanup_t> cleanup_t_foo;
    foo<bar> barFoo;

    // here I'm going to harp on one of the things I don't like about C++:
    //
    //  That so many things that look like function calls are not or that
    //  the parens cause subtle behavior changes.
    //
    //  I believe this is the reason this question was posted to 
    //  stackoverflow, so it not too far off topic.
    //  
    //  Many of these things exist because of backwards compatibility with C or
    //  because they wanted to fit in new features without adding keywords or
    //  new reserved tokens or making the parser even more complex than it already
    //  is.  So there are probably good rationales for them.
    //
    //  But I find it confusing more often than not, and the fact that there
    //  might be a rationale for it doesn't mean I have to like it...

    cleanup_t cleanup1();    // declares a function named cleanup1 that returns a cleanup_t

    cleanup_t cleanup2 = cleanup_t();   // cleanup2 is a variable of type cleanup_t that 
                                        //  is default initialized

    cleanup_t* cleanup3 = new cleanup_t;    // cleanup3 is a pointer to type cleanup_t that 
                                            //  is initialized to point to memory that is 
                                            //  *not* initialized

    cleanup_t* cleanup4 = new cleanup_t();  // cleanup4 is a pointer to type cleanup_t that
                                            //  is initialized to point to memory that *is*
                                            //  initialized (using default intialization)

    cleanup2 = cleanup_t( not_quite_a_cleanup_t_func);  // explicit type conversion using functional notation

    cleanup_t();    // the OP problem
    cleanup2();     // call the function pointed to by cleanup2
    (*cleanup2)();  // same thing

    class cleanup_class
    {
        cleanup_t cleanup5;

    public:
        cleanup_class() : 
            cleanup5() // class member default initialization
        { };
    };
}
+12

Source: https://habr.com/ru/post/1699595/


All Articles