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!
ParameterSize()that would allow it to deduceType.ParameterSize, no?Args...to beint a, char b, int c. I would do astatic_assert()inGetParameterSizeto see ifArgs...is actually getting set. I would guess it might not be.sizeof...(Args)yields 3 inGetParameterSize. To see that, I had to comment out the recursive call toParameterSize.Remainingis empty, you "call" (instantiate) in the first versionParameterSize<>()which doesn't specifyType, andTypecannot be deduced, hence the error. In the second version (with the overload), when you callParameterSize<some_type>(), both overloads are viable and neither is more specialized; hence the ambiguity.