Reputation: 523
I have been writing a project in C
, due to a third party vendor who writes C++
files with a .C
extension I have had to add -c c++
to my build. This is causing minor issues with some of my C
function pointers. I am currently getting the error
error: comparison between distinct pointer types ‘void (*)(uint32_t)’ {aka ‘void (*)(unsigned int)’} and ‘const void (*)(uint32_t)’ {aka ‘const void (*)(unsigned int)’} lacks a cast [-fpermissive]
Here is my function.
bool scheduler_remove_task(const void (*TickFct)(uint32_t period_ms)) {
bool task_removed = false;
if(TickFct != NULL) {
uint8_t i;
for(i=0; i<MAX_TASKS; i++) {
if(tasks[i].TickFct == TickFct) { // ERROR on this line
tasks[i].TickFct = NULL;
task_removed = true;
logger_log(&sch_logger, LOG_INFO, "Task %s removed", tasks[i].task_name);
break;
}
}
}
return task_removed;
}
I get the pointers are seen as different types but why does the const
keyword matter when I am only comparing values? What is different about const
in C++?
Is there a way to keep my comparison without removing const
from the arguments list?
Upvotes: 1
Views: 213
Reputation: 40791
const void (*TickFct)(uint32_t period_ms)
declares TickFct
as a pointer to a function which returns const void
. This is a different function type than void(uint32_t)
.
You want a const-pointer to a function that returns void: void (*const TickFct)(uint32_t period_ms)
.
Or that might be clearer with a type alias:
// typedef void(* tick_fc_t)(uint32_t period_ms); // C-style
using tick_fct_t = void(*)(uint32_t period_ms);
bool scheduler_remove_task(const tick_fc_t TickFct) { // ...
Upvotes: 3