cpp/language/friend

The friend declaration appears in a and grants a function or another class access to private and protected members of the class where the friend declaration appears.

Description
@1@ Designates a function or several functions as friends of this class:

@2@ (only allowed in non- class definitions) Defines a non-member function, and makes it a friend of this class at the same time. Such non-member function is always.

@3@ Designates the class, struct, or union named by the (see ) as a friend of this class. This means that the friend's member declarations and definitions can access private and protected members of this class and also that the friend can inherit from private and protected members of this class. The name of the class that is used in this declaration does not need to be previously declared. @4@ Designates the type named by the or  as a friend of this class if that type is a (possibly ) class, struct, or union; otherwise the  declaration is ignored. This declaration will not forward declare a new type.

Template friends
Both and  declarations may appear with the  specifier in any non-local class or class template (although only function templates may be defined within the class or class template that is granting friendship). In this case, every specialization of the template becomes a friend, whether it is implicitly instantiated, partially specialized, or explicitly specialized.

Friend declarations cannot refer to partial specializations, but can refer to full specializations:

When a friend declaration refers to a full specialization of a function template, the keywords and default arguments cannot be used:

A template friend declaration can name a member of a class template A, which can be either a member function or a member type (the type must use ). Such declaration is only well-formed if the last component in its nested-name-specifier (the name to the left of the last ) is a simple-template-id (template name followed by argument list in angle brackets) that names the class template. The template parameters of such template friend declaration must be deducible from the simple-template-id.

In this case, the member of any specialization of either A or partial specializations of A becomes a friend. This does not involve instantiating the primary template A or partial specializations of A: the only requirements are that the deduction of the template parameters of A from that specialization succeeds, and that substitution of the deduced template arguments into the friend declaration produces a declaration that would be a valid redeclaration of the member of the specialization:

Template friend operators
A common use case for template friends is declaration of a non-member operator overload that acts on a class template, e.g. for some user-defined.

Such operator can be defined in the class body, which has the effect of generating a separate non-template for each  and makes that non-template  a friend of its :

or the function template has to be declared as a template before the class body, in which case the friend declaration within can refer to the full specialization of  for its :