I had class which had a friend function for example if the header was:
class A{
friend void foo();
public:
A(){}
};
void foo();
So what I cannot get is what does the compiler do with the second declaration?
I've all so noticed I can write as many of them as I want with no effect, e.g
void foo();
void foo();
...
(Assume the implementation is in the cpp file)
Plus I couldn't find where does the standard say the declaring some function as a friend also declares the function itself.
If the friend
declaration were the only declaration, then the function would be in the surrounding namespace, but couldn't be found by the normal lookup rules; it could only be found by argument dependent lookup.
In this case, it means that it couldn't be found at all, since there are no arguments; so you also need a declaration in the namespace to make it accessible.
Sometimes you want that behaviour; for example, a streaming operator can be found by ADL without declaring it in the namespace:
struct A {
friend std::ostream & operator<<(std::ostream & s, A const & a) {
return s << "Hello";
}
};
A a;
std::cout << a; // finds the friend function by ADL, based on the argument 'a'
I've all so noticed I can write as many of them as I want with no effect
Yes, you can repeat declarations as many times as you like.
Plus I couldn't find where does the standard say the declaring some function as a friend also declares the function itself.
Most of the specification for declarations (C++11 clause 7) is the same whether or not the declaration contains a friend
specifier. 7.3.1.2/3 specifies that the name is in the namespace, not the class, and also specifies the lookup rules.