Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 next message
Tom1
Messages: 635
Registered: March 2007
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

Re: MemFn() only offers void return types [message #49985 is a reply to message #49886] Mon, 11 June 2018 15:53 Go to previous message
mirek is currently offline  mirek
Messages: 11991
Registered: November 2005
Ultimate Member
Tom1 wrote on Tue, 29 May 2018 11:32
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



You are absolutely right. Applied. Thank you.

Mirek
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: Tue Aug 20 20:33:36 CEST 2019

Total time taken to generate the page: 0.01240 seconds