When is it useful to know “already a friend”?

I have code that uses a preprocessor structure to generate some utility classes. Apparently, some macros cause the same friend declaration to be included twice in the class, something like this:

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

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

Bestie already a friend of Friend [enabled by default]

I do not see use in this warning. I am curious why it was included in gcc in the first place. However, since this is unlikely to meet the SO community, I will pose my question as follows: What problems can arise when the friend declaration repeats, or about probable programmer errors in this case?

The only problem I can think of is, "Perhaps you intended to write something else here instead of repeating the same, so I will warn you about it." However, in this case, the alleged friendship will be absent, which will lead to an "access control violation" error in the code in which the friendship is made, so I almost do not use the warning itself.

Are there any potential issues that I am missing?

+6
source share
1 answer

I don’t think it’s useful to announce your friend many times. I think this was the bug that was reported, and I think that they provided work . It’s better to just declare a friend to your class immediately and later duplicate it. Also check this

I think the warning was only to inform the user that he had repeatedly written redundant code, which is useless. Else I do not think there was any use of this warning. This is why most programmers have reported this as a bug.

+1
source

Source: https://habr.com/ru/post/971368/


All Articles