Best alternative to a typedef for a function template?

前端 未结 5 1563
你的背包
你的背包 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:51

    Code Example:

    #include 
    
    
    template 
    T sqr(T a){
        return a*a;
    }
    
    auto s = sqr;
    int main() {
    
        std::cout<

    prints 9, that means that used int function
    auto requires c++0x, you may use real typename instead, but it's ugly and you'll need check changing signature too. Besides, this code, may disallow your compiler to inline this function

    • As for me, it's often better use full name with <>

    • In your case you need not use <> because myFunc is enough(DataType may be got from argument type)

提交回复
热议问题