Why code below well compiled in g++ but get error on clang?
#include <iostream>
class Object {};
class Print
{
public:
template <typename CharT>
inline friend std::basic_ostream<CharT> & operator<<(std::basic_ostream<CharT> & out, const Object&)
{
return (out << "object");
}
static void f( const Object& str )
{
std::cout << str;
}
};
int main()
{
std::cout << Object() << std::endl;
return 0;
}
Proof links: g++ / clang++
When I moved friend function to global namespace, code well compiled for both compilers (clang++ / g++).
Which implementation in this case is more C++ Standart compatible?
Friend function is a function that is able to access the private and protected members of a class. In contrast, a friend class is a class which help in accessing the private members of a class. A friend function is declared by including its prototype inside the class, antecede it with the keyword friend.
Advantages of Friend Function in C++It allows the sharing of private class information by a non-member function. It accesses the non-public members of a class easily. It is widely used in cases when two or more classes contain the interrelated members relative to other parts of the program.
A friend function is a special function in C++ which in-spite of not being member function of a class has privilege to access private and protected data of a class. A friend function is a non member function or ordinary function of a class, which is declared as a friend using the keyword “friend” inside the class.
In object-oriented programming, a friend function, that is a "friend" of a given class, is a function that is given the same access as methods to private and protected data. A friend function is declared by the class that is granting access, so friend functions are part of the class interface, like methods.
Clang is correct here. Friend functions defined inside classes can only be found using argument-dependent-lookup on their arguments, not by ordinary lookup. Because Print
is not an associated scope to Object
, the operator<<
should not be found. Partial quote from the Standard:
7.3.1.2 Namespace member definitions [namespace.memdef]
3 Every name first declared in a namespace is a member of that namespace. If a friend declaration in a non-local class first declares a class, function, class template or function template97 the friend is a member of the innermost enclosing namespace. The friend declaration does not by itself make the name visible to unqualified lookup (3.4.1) or qualified lookup (3.4.3). [ Note: The name of the friend will be visible in its namespace if a matching declaration is provided at namespace scope (either before or after the class definition granting friendship). — end note ] If a friend function or function template is called, its name may be found by the name lookup that considers functions from namespaces and classes associated with the types of the function arguments (3.4.2).
As @sehe mentions, the proper way to add an operator<<
to Object
is to define it as a global function (using Object
interface) or as a friend
function inside itself (using private
functions from Object
), not in some auxiliary class. See also Herb Sutter's old GotW column "What's in a class?"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With