Why can I use `operator<<` on temporary std::ofstream objects?

There is overload which takes stream by Rvalue reference:

template< class CharT, class Traits, class T >
basic_ostream< CharT, Traits >& operator<<( basic_ostream<CharT,Traits>&& os,
                                        const T& value );

temp is passed as os. From reference.


The C++ standard mandates the following function template existing (C++17 n4659 30.7.5.5 [ostream.rvalue]):

template <class charT, class traits, class T>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&& os, const T& x);

With effects specified as os << x.

Note that the same exists for extraction (>>) as well.