Variadic Templates on Method Accepting Function Pointer

Viewed 724

The following code looks to me as though it should be working fine, but it produces

C2738: could not deduce template argument for 'Type'.

in VS2013.

template <typename ReturnType, typename...Args>
uint GetParameterSize(ReturnType(*method)(Args...))
{
    return ParameterSize<Args...>();
}

template <typename Type, typename...Remaining>
uint ParameterSize()
{
    uint output = sizeof(Type);

    if (sizeof...(Remaining) > 0)
    {
        output += ParameterSize<Remaining...>();
    }

    return output;
}

void MyMethod3(int a, char b, int c )
{
}

// Elsewhere
uint size = GetParameterSize(&MyMethod3);

I thought, "Oh maybe it's because it doesn't have a final condition." So I added:

template <typename Type>
uint ParameterSize()
{
    return sizeof(Type);
}

Which led to

C2668: 'ParameterSize' ambiguous call to overloaded function.

I mean, it looks simple enough and I think it should work. How can the compiler not deduce the arguments? I'm new to variadic templates so I might be missing something, but some help would be appreciated. Thanks!

3 Answers
Related