Search code examples
c++functionc++11using

Is it possible to use using for functions?


For example

template<class T, class U>
void f();

template<class T> using g = f<T, int>;

Or any similar idea for functions?


Solution

  • No. You cannot do that. You need to create a new function that calls f, forwarding all arguments and template arguments.

    template<class T, class U>
    void f();
    
    template<class T>
    void g() {
        f<T, int>();
    }
    

    A C++14 alternative is a variable template of function pointer type:

    template<typename T>
    void (*g)() = &f<T, int>;
    

    Although this approach ignores default arguments and probably has other quirks as well. I highly recommend the more verbose wrapping approach.