6

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?

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    IMO, code duplication is bad by default. The problem I can see is that if you declare a friend to a class at a time t of the development, and need to remove this declaration later, it's easier if the friend declaration is written just one time, and not 42 times across your whole class definition ... To conclude, I'd say that useless code should not be written, that's probably why there's a warning. – Unda Jun 26 '14 at 06:56

1 Answers1

1

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.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Yeah, I know of this bug report. It uses language like "useless warning," but I'd say "[citation needed]." I was curious if there were any cases where this warning can help, hence the question. – Angew is no longer proud of SO Jun 26 '14 at 07:04
  • @Angew:- 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. BTW I like your curiosity +1 for the question already :)! – Rahul Tripathi Jun 26 '14 at 07:06
  • No, it doesn't. It's One *Definition* Rule, not One *Declaration* Rule. – T.C. Jun 26 '14 at 07:16
  • @T.C.:- Yes I knew I missed that. Thanks! – Rahul Tripathi Jun 26 '14 at 07:17