Question

I'd like to use a function signature as a template argument. It works great for classes, but when I try the same trick for function templates, msvc throws an error:

error C2768: 'Func' : illegal use of explicit template arguments

Here's my code:

template <typename Signature>
void Func();

template <typename R, typename A1>
void Func<R(A1)>();

What should I do to make it work?

Was it helpful?

Solution

You cannot partially specialize a function template, that is not supported by the language. What you can do is to create a partially specialized class template with a static member function, and possibly a trampoline function that would instantiate that class template and invoke the static function.

Something like this:

namespace detail
{
    template<typename Signature>
    struct helper;

    template<typename R, typename A1>
    struct helper<R(A1)>
    {
        static void call()
        {
            // Do stuff with R and A1...
        }
    };
}

template<typename Signature>
void Func()
{
    detail::helper<Signature>::call();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top