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 » Community » Newbie corner » [SOLVED] Do Value can store anonymous function ?
[SOLVED] Do Value can store anonymous function ? [message #52765] Wed, 20 November 2019 13:55 Go to next message
Xemuth is currently offline  Xemuth
Messages: 310
Registered: August 2018
Location: France
Senior Member
Hello community,

I'm wondering if value can store something like this :

#include <Core/Core.h>

using namespace Upp;

int testAddition(const int& a,const int& b){
	return a+b;	
}

CONSOLE_APP_MAIN
{
	int(*fn)(const int& ,const int&);
	fn = (int(*)(const int& ,const int&)) &testAddition;
	Cout() << fn(2,3) << EOL; //Working
	
	void* fn2 =(void*) fn;
	Cout() << ((int(*)(const int& ,const int&))fn2)(1,1) << EOL; //Working
	
	Value v = fn2;

	Cout() <<  static_cast<int(*)(const int& ,const int&)>( v.Get<void*>())(2,3) << EOL; //ambiguous conversion from Nuller to //Void*  & Invalide static_cast from void* to int(*)(const int&, const int&)
	Cout() << v.Get<Nuller>()(2,3) <<EOL; // no match for call to (const Upp::Nuller)(int,int) Logique....
	Cout() << ((int(*)(const int& ,const int&))v.Get<Nuller>())(2,3) <<EOL; //invalid cast
}


Using Value to store a ptr to function ?

Thanks in advance.


[Updated on: Wed, 20 November 2019 17:19]

Report message to a moderator

Re: Do Value can store anonymous function ? [message #52768 is a reply to message #52765] Wed, 20 November 2019 17:12 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 828
Registered: August 2007
Experienced Contributor
Hello Xemuth,

RawPickToValue or RawToValue (depending on your case) should work with Function objects.
You can use One<> or store pointers to functions or objects in Value using the RawPickToValue or RawToValue, as well.

	Event<> WhenFunction = [=] { Cout() << "Hello world.\n"; };
	Value v = RawPickToValue(pick(WhenFunction));
	const auto& fn = v.To<Event<>>();
	fn();


Best regards,
Oblivion


[Updated on: Wed, 20 November 2019 17:16]

Report message to a moderator

Re: Do Value can store anonymous function ? [message #52769 is a reply to message #52768] Wed, 20 November 2019 17:19 Go to previous message
Xemuth is currently offline  Xemuth
Messages: 310
Registered: August 2018
Location: France
Senior Member
Perfect !

Thanks Oblivion !


Previous Topic: [solved] Widget events - any other than WhenAction ?
Next Topic: [SOLVED] ERROR: Not in table (destructor clobbered?)
Goto Forum:
  


Current Time: Mon Jan 25 20:38:38 CET 2021

Total time taken to generate the page: 0.01337 seconds