c++class

Any way to declare multiple friend classes in one statement?


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{};

Solution

  • 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.