Best alternative to a typedef for a function template?

前端 未结 5 1569
你的背包
你的背包 2021-02-07 03:59

What I\'d like to do is something like this:

template 
DataType myFunc(DataType in)
{
   ...
}

typedef myFunc myFunc_i;

myFunc         


        
5条回答
  •  天涯浪人
    2021-02-07 04:46

    Try this:

    typedef int (*myFunc_i_type)(int);
    myFunc_i_type const myFunc_i = &myFunc;
    

    This will trigger a compiler error if the signature of myFunc ever changes. Making it const also prohibits reassigning. Function pointers are also callable like any normal function: myFunc_i(5).

    If you don't want a compiler error but rather have it update itself, use auto (again with const):

    auto const myFunc_i = &myFunc;
    

提交回复
热议问题