Unused friend class in C++
Is there a way to detect (for instance with compiler warning) if classes are declared friend
but do not access private members, ie. when fri开发者_StackOverflow中文版endship is useless?
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.
Compiler 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.
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.
精彩评论