Bug with 'can be made const' check

First let me say that I'm sorry if this is not the right place to post this.

If a class member function which could otherwise be marked const is part of an array of function pointers for which the prototype is not const, marking the function const will result in a compile error.  ReSharper C++ fails to recognize this.

 

Example:

 

class MyClass

{

  void HandleA() { m_SomeVal++; }

  void HandleB() { assert(m_SomeVal > 0); }

  void HandleC() { m_SomeVal--; }

};

 

HandleB() could be marked const, unless I also have this:

 

struct

{

  int op;

  void (*MyClass::handler)();

} handlers[] =

{

  1, &HandleA,

  2, &HandleB,

  3, &HandleC

}

 

Hopefully this is clear despite my syntax errors :P

1 comment

Hello William.

This is how the analysis is implemented - when checking if a member function can be made const, R++ looks only at the function body, since going through all usages of the function would be very time consuming. Unfortunately, this means some false positives are possible, like in your example.

Thanks for the feedback!

0

Please sign in to leave a comment.