2

The standard library provides many useful "is" typetraits that tell about the relationship of two classes, such as is_base_of, is_convertible, is_same, etc. Is there any way to implement a typetrait is_friend to tell whether class A is a friend of class B?

Sample code:

class A {};
class B { friend class A; };
class C { friend class B; };

is_friend<A, B>; // true
is_friend<B, A>; // false
is_friend<B, C>; // true
is_friend<A, C>; // false
Nighteen
  • 731
  • 1
  • 7
  • 16
  • 1
    I don't know but what would be the point since friend is a hint for compiler? AFAIK, it doesn't generate any code. –  May 20 '19 at 21:04
  • I have implemented something like this -> https://stackoverflow.com/a/11748131/2565020 To check whether a class is reflectable, instead of checking for the presence of a member/function or creating a flag, I thought it would be more beautiful to check whether the reflectable class is a friend of `reflector` – Nighteen May 20 '19 at 21:09
  • @Nighteen May be [this](https://stackoverflow.com/questions/27492132/how-can-i-remove-refactor-a-friend-dependency-declaration-properly) is helpful to some extend. – πάντα ῥεῖ May 20 '19 at 21:32
  • 1
    Is there any use case for that? – arsdever May 20 '19 at 21:53

1 Answers1

0

No, it’s not possible. In your example, nothing in B has any access restrictions anyway, so A’s friendship has no effect. Since friendship is neither inherited nor transitive, there’s no way to use a trick like inheriting from the class in question and adding a private member.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76