Typedef with template functions

Let's say I have a template function in namespace A. I also have another namespace B. In namespace A a template function is declared which is defined as

template<typename T, typename U>
void f(T a, U b);

      

Now in namespace B, I would like to declare a specialized template function type. I was thinking if I could typedef

use the template function, so it is declared in the B namespace as

void f(int a, double b);

      

without actually implementing the function calling the template function. As there is a way to declare new name types with specific template parameters, shouldn't there be a way to do this with functions? I've tried different methods to achieve it, but it didn't quite work.

So, is there a way in C ++ to override a function with given template parameters without actually implementing the new function? If not, is this possible somehow achievable in C ++ 11?

It would be a neat function, because it would make the purpose of the function clearer and it would be syntactically better :)

Edit: one could write:

using A::f<int, double>;

      

in namespace B and the function will render with these template parameters

+3


source to share


2 answers


You can use using

:

namespace A {
    template <typename T> void f(T);
    template <> void f<int>(int);    // specialization
}

namespace B {
    using ::A::f;
}

      



You cannot distinguish between such specializations (since it using

only refers to names), but it should be enough to make the desired specialization visible.

+5


source


You can wrap the inline function:

namespace A
{
    template<typename T, typename U>
    void f(T a, U b);
};

namespace B
{
    inline void f(int a, double b) { A::f(a,b); }
};

      



See this question:

C ++ 11: How is a function alias?

+2


source







All Articles