3

I would like to know if it is possible to write a trait capable to detect a friend class declaration. I'm particularly interested in the case of forward declaration, like so:

struct Baz
{
   friend struct Foo; 
};

struct Foo {};

Please no Boost or other external libraries. The answer should stick either with the standard (C++17) or a custom trait.

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 1
    What would you do with this ability? It doesn't seem reasonable to SFINAE something around such a construct. – Nicol Bolas Mar 22 '21 at 14:51
  • @NicolBolas I'm writing a reflection library based on template metaprogramming. In order to allow the reflection system to inspect a type (within its private member) a friend declaration must be done: `template friend struct reflection::ReflectedTypeInternal ` I would like to detect if that friend declaration is present in order to eventually throw compile time errors and ease the use of the library. – Antonio Cantarella Mar 22 '21 at 15:00
  • Wouldn't you throw a compile error when you try and fail to reflect into the type? – Nicol Bolas Mar 22 '21 at 15:07
  • @NicolBolas I do that already. But detecting that friend declaration would allow me to throw better readable errors in early stages of the compilation. Otherwise I have to rely in later stages compilation errors which are less understandables. – Antonio Cantarella Mar 22 '21 at 15:15
  • You could create a helper macro which generates this friend declaration and also some hidden tag type, which you can detect later. I don't think you can detect the friend itself. – rustyx Apr 16 '21 at 07:03

0 Answers0