Go to main content
Oracle® Developer Studio 12.6: C++ User's Guide

Exit Print View

Updated: July 2017
 
 

4.9 Declaring a Static Namespace-Scope Function as a Class Friend

The following code is invalid.

class A {
  friend static void foo(<args>);
  ...
};

Because a class name has external linkage and all definitions must be identical, friend functions must also have external linkage. However, when you use the -features=extensions option, the compiler to accepts this code.

Presumably the programmer’s intent with this invalid code was to provide a nonmember “helper” function in the implementation file for class A. You can get the same effect by making foo a static member function. You can make it private if you do not want clients to call the function.


Note -  If you use this extension, your class can be “hijacked” by any client. Any client can include the class header, then define its own static function foo, which will automatically be a friend of the class. The effect will be as if you made all members of the class public.