Menu

Suggesting const pointer on callback function (cppcheck 2.11)

2023-07-07
2023-07-07
  • Martin Poupě

    Martin Poupě - 2023-07-07

    Hello,
    I use functions, which I register as a callback (I set its address to a pointer to function). Sometimes the functions use the parameter(s) only for reading. Cppcheck 2.11 will suggets for that case:

    style: Parameter 'pContext' can be declared as pointer to const. However it seems that 'Function' is a callback function, if 'pContext' is declared with const you might also need to cast function pointer(s). [constParameterCallback]

    I think this might bring more problem. When I forcibly cast the function address to match the pointer, it might hide more serious mismatch (e.g. the pointer to function type is changed and I rely on compiler to do the checks for me). Such mismatch will do a crash, which might be difficul to find, if the handler is called only "sometimes".
    I think the Cppcheck should be more silent for such cases.
    Best Regards Martin

     
  • CHR

    CHR - 2023-07-07

    I agree that it is unfortunate that casting is mentioned in the the error message. The idea is that constParameterCallback is a separate ID which can be suppressed as needed.

     
  • Martin Poupě

    Martin Poupě - 2023-07-07

    OK, I understood. Thank you for help and hint to suppress constParameterCallback

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.