Search code examples
c++assertfriend

assert as a friend class


I can not declare neither assert class, nor assert function as a friendly to my class.

Am I right with such a declaration?

class Baka
{
private:
friend assert;          //invalid friend declaration
friend void assert();   //expected an identifer
public:
}

I've already googled a lot, but I can't find right arguments for assert() and the real name of the "assert" class( I will appreciate any help.


Solution

  • assert isn't a function, it's a macro. Therefore if you have <cassert> included upstream it will be expanded by the pre-processor before compilation

    // cassert file
    // Not debug version
    #define assert(_Expression)     ((void)0)
    

    So your class actually says

    class Baka
    {
    private:
      friend ((void)0);          //invalid friend declaration
      friend void ((void)0)();   //expected an identifer
    public:
    }
    

    Which obviously shouldn't compile.