Reputation: 1915
Given this code I have a compiler error I've had trouble solving:
struct TestType
{
void PrintX( void )
{
printf( "TestType::PrintX : %d\n", x );
}
int x;
};
// Error here, BuildFunction overload not found
Function f = BuildFunction<decltype( &TestType::PrintX ), &TestType::PrintX>( &TestType::PrintX );
// Here is the overload
template <typename FunctionType, FunctionType *FunctionPtr, typename C>
Function BuildFunction( void (C::*fn)( void ) )
{
return Function( fn, &CallMethodVoid<FunctionType, FunctionPtr, C> );
}
My exact error message is:
1>main.cpp(141): error C2893: Failed to specialize function
template 'Function BuildFunction(void (__thiscall C::* )(void))'
1> With the following template arguments:
1> 'void (__thiscall TestType::* )(void)'
1> 'TestType'
This code works just fine with static C functions (and the appropriate overloads), I just have this issue when trying to deal with a class or struct method. Any ideas on what the problem may be?
Upvotes: 1
Views: 1757
Reputation: 7904
The problem lies in the type of the second template parameter of BuildFunction<>
.
The first template parameter FunctionType
, deduces to void (TestType::*)(void)
since you've passed decltype(&TestType::PrintX)
as the first template argument. The second template parameter as you specified it, reads FunctionType *
. So the second template parameter is expecting a void (TestType::**)(void)
(a pointer to a pointer to member function), which I don't think is what you intended. If you change the second template parameter to be FunctionType
instead of FunctionType *
, I think you'll get what you expect.
Upvotes: 4