The operator<<
in question is a member of std::basic_ostream
:
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ostream {
public:
basic_ostream<charT,traits>& operator<<(
basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&));
// ...
};
}
Since the call is to std::cout << std::endl
, or equivalently std::cout.operator<<(std::endl)
, we already know the exact instantiation of basic_ostream
: std::basic_ostream<char, std::char_traits<char>>
, aka std::ostream
. So the member function of cout
looks like
std::ostream& operator<<(std::basic_ostream<char, std::char_traits<char>>& (*pf)
(std::basic_ostream<char, std::char_traits<char>>&));
This member function is not a function template, just an ordinary member function. So the question remaining, is can it be called with the name std::endl
as an argument? Yes, initializing the function argument is equivalent to a variable initialization, as though we had written
std::basic_ostream<char, std::char_traits<char>>& (*pf)
(std::basic_ostream<char, std::char_traits<char>>&) = std::endl;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…