Trying to make a class
friends with an extern "C"
function, this code works:
#include <iostream>
extern "C" {
void foo();
}
namespace {
struct bar {
// without :: this refuses to compile
friend void ::foo();
bar() : v(666) {}
private:
int v;
} inst;
}
int main() {
foo();
}
extern "C" {
void foo() {
std::cout << inst.v << std::endl;
}
}
But I was very surprised to find that with g++ 4.6.1 and 4.4.4 I have to explicitly write ::
in friend void ::foo();
otherwise the friendship doesn't work. This ::
is only needed when it's extern "C"
though.
- Is this a compiler bug/problem? I wasn't expecting that behaviour.
- If it isn't a bug why is this required, but only when it's
extern "C"
and not without it? What about the name lookup rules changes that makes this necessary?
I'm stumped. There is probably some rule for this that I can't find.