I've written an object that stores a std::function<void(void*)>
, which is passed in as an argument to the constructor. The object will later call back this std::function at some point in the future. This is implemented and working great.
In each class which uses this object, they call the constructor in the initialization list like so:
mCallbackObj(std::bind(&MyClass::MyFunc, this, _1))
However, I've found that every class which contains this object as a member is increasing my codespace by ~2K. With potentially hundreds of places where this object will be used, and limited codespace options (this is an embedded product), a 2k hit per use isn't acceptable.
One interesting observation is that if a class has a second object:
mCallbackObj2(std::bind(&MyClass::MyOtherFunc, this, _1))
this only increases codespace by some ~150 bytes - very acceptable! It is only when the object is used in different classes that I see the 2K hit. Putting the classes all in one .cpp file doesn't help - still a 2k hit per class that contains this object.
I've played around with using extern template class std::function<void(void*)>;
, but this had no impact on ROM size.
I am using gcc 4.8. I'd love to use std::function
and std::bind
, but am about to give up and switch to class method pointers. They wouldn't be nearly as clean, but hopefully more ROM efficient.
Before I give up on this, are there any other options to help reduce my template codespace bloat?