Is there a way to detect (for instance with compiler warning) if classes are declared friend
but do not access private members, ie. when friendship is useless?
views:
170answers:
4Compiler warnings are not standardised, so this depends on your specific compiler(s). I would be very surprised if any of them supported this, however. A similar situation would be if you had a public member function which was only called by other public members (meaning it needn't be public), and once again I don't think any compilers detect this.
Doing either of these tests would mean extra work for the compiler writers, and I doubt if they would see them as sufficiently useful to implement.
Not that I know of. Maybe there's a refactoring tool out there that can do it. You can always try removing the friendship and see if it still compiles, but that might be time consuming for a large project.
I don't know how to detect this using compiler warnings but another way of doing this would be to go to your class definition file and do a search & replace for friend class
with /*friend*/ class
and see if it still compiles. Of course, this could get rather tedious for a large project.
You could compile the code to see that it compiles, then remove all 'friend' declarations (perhaps programmatically with sed) and sees if it still compiles.