U++ framework
Do not panic. Ask here before giving up.

Home » U++ Library support » U++ Core » MemFn() only offers void return types
MemFn() only offers void return types [message #49886] Tue, 29 May 2018 11:32 Go to previous message
Tom1
Messages: 1319
Registered: March 2007
Ultimate Contributor
Hi,

It seems to me that MemFn() (and therefore THISFN()) do not work correctly with return types. For some reason MemFn() uses Event instead of Function and therefore only offers void return types.

Switching to using Function instead of Event fixes this behavior, but I'm not sure if it is correct fix or causes trouble elsewhere...:

template <class Ptr, class Class, class Res, class... ArgTypes>
Function<Res (ArgTypes...)> MemFn(Ptr object, Res (Class::*method)(ArgTypes...))
{
	return [=](ArgTypes... args) { return (object->*method)(args...); };
}


Update: The code for MemFn() can be found in Core/Function.h ...

Update 2: Testcase:
#include <Core/Core.h>

using namespace Upp;

// Temporary fix:

template <class Ptr, class Class, class Res, class... ArgTypes>
Function<Res (ArgTypes...)> MemFn2(Ptr object, Res (Class::*method)(ArgTypes...))
{
	return [=](ArgTypes... args) { return (object->*method)(args...); };
}

#define THISFN2(x)   MemFn2(this, &CLASSNAME::x)


// Testcase for MemFn

class MemFnTest{
	typedef MemFnTest CLASSNAME;

	String fn1(double a, double b, String &c){
		String s=Format("%f %f %s\r\n",a,b,~c);
		return s;
	}
	
public:
	Function<String (double,double,String&)> fn;
	
	MemFnTest(){
		fn=THISFN2(fn1);
		fn=THISFN(fn1);
	}
};

CONSOLE_APP_MAIN{
	MemFnTest x;
	String s = x.fn(1,2,String("Hi there!"));
	printf(s);
}



Best regards,

Tom

[Updated on: Tue, 29 May 2018 11:59]

Report message to a moderator

 
Read Message
Read Message
Previous Topic: [BUG]TcpSocket::Connect return always true
Next Topic: Core Http API change proposals - HttpStatus and HttpMethod as enum class
Goto Forum:
  


Current Time: Sun May 24 19:43:36 GMT+2 2026

Total time taken to generate the page: 0.00595 seconds