std::endl
is a function template declared (27.7.3.8):
template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
The reason that you can "stream" it to std::cout
is that the basic_ostream
class template has a member declared:
basic_ostream<charT,traits>& operator<<
( basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&) );
which is defined to have the effect of returning pf(*this)
(27.7.3.6.3).
std::endl
without parentheses refers to a set of overload functions - all possible specializations of the function template, but used in a context where a function pointer of one particular type is acceptable (i.e. as an argument to operator<<
), the correct specialization can be unambiguously deduced.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…