Typedeffing a function (NOT a function pointer)

前端 未结 6 2240
醉酒成梦
醉酒成梦 2020-12-17 05:20
typedef void int_void(int);

int_void is a function taking an integer and returning nothing.

My question is: can it be

6条回答
  •  天涯浪人
    2020-12-17 05:46

    I think it's legal - the following demonstrates its use:

    typedef void f(int);
    
    void t( int a ) {
    }
    
    int main() {
        f * p = t;
        p(1); // call t(1)
    }
    

    and actually, this C++ code compiles (with g++) & runs - I'm really not sure how kosher it is though.

    #include 
    
    typedef void f(int);
    
    void t( int a ) {
        printf( "val is %d\n", a );
    }
    
    int main() {
        f & p = t;   // note reference not pointer
        p(1);
    }
    

提交回复
热议问题