Skip to main content
gamedev.net
🔒 Locked

Functor problems

Started by Matsen Oct 30, 2002 at 4:05 PM 0 replies 300+ views
Original Post
Matsen
Matsen
Hi Im trying to write a functor class, but ran into problems when the "stored" function returns void. My functor class has an invoke()-function and overloads the () operator. The return type is specified by the class template:
template << typename ObjectType, typename ReturnType >>
class Functor
{
.
.
	ReturnType operator()()
	{
		return invoke(); // forward to invoke...
	}

	ReturnType invoke()
	{
		return (object->*method)();
	}
};
  
As this is not possible in MSVC++ 6.0: void f() {} void g() {return f();} ... creating a functor with void as the return type does not compile:
Functor << MyClass, void >> fun; // error C2562: 'invoke' : 'void' function returning a value
  
Is there a way around this? I think boost's function can do this, but I don't understand how. Regards Mats Edit: The template parameters disappeard [edited by - Matsen on October 30, 2002 5:08:43 PM]
civguy
civguy
Dunno if this kind of template specialization would work

    
template <typename ObjectType, typename ReturnType>
class Functor<ObjectType, void>
{
void operator()() {
invoke();
}

void invoke() {
(object->*method)();
}
};


[edited by - civguy on October 30, 2002 5:17:59 PM]

Topic Locked

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

Sign in to reply to this topic.