Operators can be implemented in different ways, in particular an operator<<
for which the left hand side is your type can be implemented as either a free function or as a member function of that left hand side type.
While you must implement ostream& operator<<(ostream&, MyType const&)
as a free function (since MyType
is not the left hand side), the library implementation can choose* to implement operator<<
for some fundamental types insde the std::ostream
type (which is really a particular instantiation of a template, I am trying to ignore the details).
Edit: After checking with the standard this is incorrect:
This is what you are noticing in the code, the overload that takes a const char*
is implemented as a member of ostream
(basic_ostream<char,char_traits<char>
).
The overloads taking manipulators are implemented as member functions (Q2), and there is an implicit conversion from const char*
to const void*
that will be picked if you use the syntax for explicitly calling a member operator (Q1). For Q3, the answer would be:
operator<<(std::cout, "Hello").operator<<(std::endl);
* The implementation is actually not free to choose, since the standard mandates the signatures.