问题
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:
Unless the class of which val1 and val2 are instances of has base classes that themselves implement
operator>
I suggest you remove your definition ofoperator>
from the header and cpp files and recompile. This should give you a list of all calls tooperator>
guaranteed by the compiler.Boost.Operators may help to avoid such errors in the future. It can automatically provide
operator!=
if you provideoperator==
e.g., the same goes foroperator<=
,operator>
andoperator>=
if you provideoperator<
.It is extremely hard to find all calls to overloaded operators in code due to templates and the precompiler: C++ IDE for Linux with smart reference searching
来源:https://stackoverflow.com/questions/2516308/caller-graph-for-overloaded-operators-in-visual-studio-2005