I have looked at a lot of posts on arrays of member function pointers, but none that quite answer exactly for me. This is an embedded application, with no functional or dynamic allocation.
Basically I want a constant (in flash program memory) array of structures, each of which contain some member function pointers to private members. I would prefer not to manually specify the size of the array.
I have simplified it down to this example. This does not compile due to constexpr evaluation before class definition is complete. I am guessing this means before the class "Example" definition is incomplete.
Can someone please help show me how I could achieve this? Preferably without any complex programming if possible.
#include <array>
class Example
{
private:
bool SetOne(const char * strVal);
bool SetTwo(const char * strVal);
struct Param
{
using SetValueFunc = bool (Example::*)(const char * strVal);
constexpr Param(const char * n, SetValueFunc s) : Name(n), SetValue(s) {}
const char * Name;
SetValueFunc SetValue;
};
static constexpr std::array cSupportedParameters =
{
Param("One", &Example::SetOne),
Param("Two", &Example::SetTwo)
};
};
Instead of using an array for this, you can define a function that returns the array:
static constexpr std::array<Param, 2> cSupportedParameters()
{
return {
Param("One", &Example::SetOne),
Param("Two", &Example::SetTwo)
};
}
As for deducing the array size automatically, I can't think of a clean way to do that. At least if you get the count wrong with respect to how many entries are in the initializer list, it will result in a compilation error. So it should be simple to keep this up to date when you add methods.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.