Any way to declare multiple friend classes in one statement?

Viewed 339

I wish to declare multiple C++ friend classes in one statement as below, just to tidy the code. The reason is, in my actual application, there will be three or four friend classes, so it'll be messier than this sample.

Is it possible in any way?

class A{
    friend class B, C; // this doesn't work
    // friend class B;
    // friend class C; // these two would be okay
};

class B{};
class C{};
2 Answers

No you need to write it out longhand.

I can't think of a grammatical reason why your notation couldn't be introduced. (C++17 allows you to shorten the namespace notation, for example, so you're idea is not without precedent).

Why don't you propose your idea to the C++ standards committee?

Of course there will be counter-arguments centred around compiler complexity.

Whole classes - you can't, as friend declaration in that case also acts like forward declaration and that would be conflicting syntax.

But you can declare members of various classes as friends in one statement, if those are already complete:

#include "class_b.h"
#include "class_c.h"

class A {
   friend B::B(), C::~C();
};
Related