How is possible to deduce function argument type in C++?

前端 未结 5 621
南旧
南旧 2020-12-20 13:58

Having the function definition:

void f(int) { }

I want to define:

int a;

but if the function definition c

5条回答
  •  情书的邮戳
    2020-12-20 14:23

    One of approaches is to use typedef for the type of the function parameter. For example

    typedef int TParm;
    
    void f( TParm );
    
    TParm a;
    

    You can select any name for the type. For example parm_t and so on. It is important that there will not be a name collision.

    In this case you will need to change only the typedef if you want to change the type of the parameter.

    Or if your compiler supports aliases you can also write

    using TParm = int;
    
    void f( TParm );
    
    TParm a;
    

    Also you can wrap the function in a namespace or class.:) For example

    struct IFunction
    {
       typedef int parm_t;
       static void f( parm_t = parm_t() ) {}
    };
    
    //...
    
    IFunction::parm_t a;
    IFunction::f( a );
    

提交回复
热议问题