Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is "already a friend" warning useful?

I have code which uses a preprocessor-heavy framework to generate some utility classes. Apparently, some of the macros result in the same friend declaration being included twice in a class, something like this:

class Friendly {
  // :::
  friend class Bestie;
  friend class Bestie;
  // :::
};

When built with gcc (4.8.1), it generates a warning like

Bestie is already a friend of Friendly [enabled by default]

I can't really see a use in this warning. I'm curious why it was included in gcc in the first place. However, as that is hardly answerable by the SO community, I'll state my question like this: What problems could arise from a friend declaration being repeated, or what likely programmer errors could such an occurrence indicate?

The only problem I can think of which this could be hinting at is "You may have intended to write something else here instead of the same thing again, so I will helpfully warn you about this." However, in such case, the intended friendship would be missing, which would lead to an "access control violation" error in the code exercising the friendship, so I see little use of the warning itself.

Are there any potential problems I'm overlooking?

like image 960
Angew is no longer proud of SO Avatar asked Jun 26 '14 06:06

Angew is no longer proud of SO


1 Answers

I dont think it is usefull to declare your friend class many times. I think it was a bug which was reported and I think they have provided a work around. It is better to just declare a friend to your class at once and should avod the duplication later. Also check this

I think the warning was only to inform the user that he has written redundant code many times which is of no use. Else I dont think that there was any use of this warning. That is why most of the programmers reported it as a bug.

like image 197
Rahul Tripathi Avatar answered Nov 20 '22 08:11

Rahul Tripathi