Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with templates and function pointers

Started by Axiverse Dec 6, 2005 at 1:36 PM 1 replies 600+ views
Original Post
Axiverse
Axiverse
I have this test code that is not working:

template <int (*function)(int)>
int callMe(void* object, int value)
{
	return (*function)(value);
}

template <class type>
class Calls;

template <class result, class type1>
class Calls <result(type1)>
{
public:

	template <result (*function)(type1)>
	static result callMe(void* object, type1 value)
	{
		return (*function)(value);
	}
};


int yo (int i)
{
	return i++;
}

int main(int argc, char* argv[])
{
	Calls<int(int)>::callMe<yo>(NULL, 1);
	callMe<yo>(NULL, 1);
	return 0;
}

The problem is that even though callMe works, Calls::callMe does not, I get this compile error

error C2440: 'specialization' : cannot convert from 'int (__cdecl *)(int)' to 'result (__cdecl *const )(type1)'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast

Cocalus
Cocalus
Well it compiles with GCC 3.4.2. What compiler are you using. I'm not versed in some of the template syntax your using (I only have a basic knowledge of templates though). So I can't tell if it's standards compliant or if it needs some extension in GCC (I'd guess standard).
stormrunner
stormrunner
Quote:
Original post by Cocalus
So I can't tell if it's standards compliant or if it needs some extension in GCC (I'd guess standard).

It would appear that it is standard, as it also compiles fine with GCC 4.0.
stormrunner

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.