I have read in this that in C it must be safe (based on the answers)
And I was wondering if it is safe in C++ to do the same (based on answers in this, it should be). I have attached my code which compiles to show exactly what I am trying to ask.
I have a function that has a function pointer as its argument. The argument is a function pointer with nonconst arguments but I supply that function with a pointer to a function with const arguments.
GCC did not complain about it and I think it should be safe but want to make sure I am not leaving any details.
typedef uint16_t (myFunction)(uint16_t);
uint16_t increment(const uint16_t input);
uint16_t callFunction(myFunction* function, const uint16_t input);
uint16_t increment(const uint16_t input)
{
return input + 1;
}
uint16_t callFunction(myFunction* function, const uint16_t input)
{
return (*function)(input);
}
void setup()
{
callFunction(increment, 2);
/* add setup code here */
}
void loop()
{
/* add main program code here */
}