If I have a function defined as friend inside a class. What is the namespace of that function?
namespace A{namespace B{
struct S{
friend void f(S const&){};
};
}}
int main(){
A::B::S s{};
f(s); // ok
::f(s); // not ok, no f in global namespace
A::B::f(s); // no f in A::B
A::B::S::f(s); // not ok, no f in A::B::S
}
Does it even have a namespace? Does it make sense for it to have a namespace?
What if I want to disambiguate a call in the commonly used idiom?
using std::swap;
swap(a, b);
Am I forced to define it outside the class and declare it a friend?
f(s)
works because of argument-dependent lookup. The function is declared in the scope ofA::B::S
, but it's not a member of the class. – Some programmer dudef
is inA::B
(same namespace as its argument)? – Walterstd::swap
to be considered in the lookup (for types other than those instd
) you must bring it into the local scope. – WalterA::B
, not inA::B::S
. It is just not visible to ordinary name lookup. – AnT