views:

31

answers:

1

Is is possible to get a callers graph for overloaded operators?

I have a simple struct with a natural ordering which I have represented by overloading the relational operators. Looking back through the code, it appears that I made a mistake in defining operator >. I had set the greater than to simply return the negation of operator < (this is not correct as this will mean that (val1 > val2) == true when val1 == val2).

Anyway before fixing this, I want to check where the > operator is called in the rest of the code to make sure there are no unintended consequences. This does not seem to be possible using the Visual Studio 2005 call browser. When I search for the function, it recognises where it is defined in the code, but lists there being no calls to or from that function, which is not the case.

Aside from searching through all instances of ">" in my project code do I have any other options?

This page - http://msdn.microsoft.com/en-us/magazine/cc163658.aspx#S3 - indicates that detecing operator calls is not something that was originally in VS2005, but I can't tell if this has changed.

+1  A: 
  1. Unless the class of which val1 and val2 are instances of has base classes that themselves implement operator> I suggest you remove your definition of operator> from the header and cpp files and recompile. This should give you a list of all calls to operator> guaranteed by the compiler.

  2. Boost.Operators may help to avoid such errors in the future. It can automatically provide operator!= if you provide operator== e.g., the same goes for operator<=, operator> and operator>= if you provide operator<.

  3. It is extremely hard to find all calls to overloaded operators in code due to templates and the precompiler: http://stackoverflow.com/questions/2514350/2514529#2514529

Sebastian
Good answer, not only does it provide a solution to the specific problem (find all instances of `operator>`) but gives advice on the secondary issue of how to avoid getting in to this problem in the first place, and finally links to some background information on the topic.With the templates, it looks like a case of the language getting ahead of the tools and will eventually be worked through. The precompiler on the other hand...
Rodion Ingles
In the `<utility>` header there are templated operators for relational operators (see http://www.cplusplus.com/reference/std/utility/rel_ops/) that do the same as the Boost operators in 2).
Rodion Ingles