Is it possible to declare a friend function as static?

Jeremy Friesner picture Jeremy Friesner · Nov 7, 2013 · Viewed 23.7k times · Source

Here is some C++ example code that compiles and works fine:

class A
{
public:
   A() {/* empty */}

private:
   friend void IncrementValue(A &);
   int value;
};

void IncrementValue(A & a)
{
   a.value++;
}   

int main(int, char **)
{
   A a;
   IncrementValue(a);
   return 0;
}

What I would like to do, however, is declare IncrementValue() as static, so that it can't be seen or called from another compilation unit:

static void IncrementValue(A & a)
{
    a.value++;
}

Doing that, however, gives me a compile error:

temp.cpp: In function ‘void IncrementValue(A&)’:
temp.cpp:12: error: ‘void IncrementValue(A&)’ was declared ‘extern’ and later ‘static’
temp.cpp:8: error: previous declaration of ‘void IncrementValue(A&)’

... and changing the friend declaration to match doesn't help:

friend static void IncrementValue(A &);

... as it gives this error:

temp.cpp:8: error: storage class specifiers invalid in friend function declarations

My question is, is there any way in C++ to have a (non-method) friend function that is declared static?

Answer

Praetorian picture Praetorian · Nov 7, 2013

Quoting N3691 - §11.3/4 [class.friend]

A function first declared in a friend declaration has external linkage (3.5). Otherwise, the function retains its previous linkage (7.1.1).

So you need to declare the function as static prior to declaring it as a friend. This can be done by adding the following declarations above the definition of A.

class A;  // forward declaration, required for following declaration
static void IncrementValue(A&); // the friend declaration will retain static linkage