I have read the reasons why inheriting from std::function is a bad idea. I can live with no virtual destructor: there is only one place in my program where the subclass needs to be deleted, and I know exactly where that is, so I'm happy to do the manual delete.
I wouldn't need to do this at all if I could access target() - I need the address of the "wrapped" function so I can find it again in a container. I'm using Arduino IDE 1.8.3 (c++11 standard) and targeting NodeMCU, Wemos D1, Sonoff etc...hardware is not THAT relevant suffice to say that memory is very limited. thus the stdlib is a "cut-down" version. For example, the compiler flags specify no RTTI and - of course - target() etc are only compiled in if RTTI is present. I un-hacked that and I couldn't even compile as the resultant executable was too big for the available RAM...
The thing is I don't need fancy typeinfo, just the physical address of the target callable.
My idea was to inherit from std::function (for the only function signature used in the app which is function<void()>
, override the constructor, capture the address of the target and provide a function (NOT called target() so as not to confuse) to retrieve it at the appropriate time - "getFn()" maybe...It's what I'm already doing with my own "task" class and it works fine. I need to extend it for std::function but I can't see how I can do it unless I can access the "target" address!
Finally, to preclude any trying-to-be-helpful replies that suggest e.g. returning a "handle" to the user routine that creates this function in the first place and then having them call again with the handle to find the container entry...I absolutely don't want to do it that way. I have made a rod for my own back as my API must avoid receiving any handles and then using them to get back to the original. That can't change, so I'm not looking for alternative scenarios, hence the lack of more detail of the exact implmentation.
I'm only asking for advice re inheriting from std::function.
In summary then, I guess my question is: are there any other reasons why this might not work, given that I think I can handle the problems I have already read about?
This is what I had in mind:
class XFunction: public function<void()> {
public:
uint32_t fn;
XFunction(void (&_fn)() ): function<void()>(_fn) {
Serial.printf("CTOR %08x fn=%08x\n", this,_fn);
fn=reinterpret_cast<uint32_t>(&_fn);
}
uint32_t getFn(){
return fn;
}
};