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 |
Tom1
Messages: 1212 Registered: March 2007
|
Senior 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
|
|
|
Goto Forum:
Current Time: Tue Apr 23 10:26:20 CEST 2024
Total time taken to generate the page: 0.02753 seconds
|