2 回答
You can use
static void Dump( std::ostream& os, Book& b, format_f f )
{
f(os); os << b;
}
(Just f(os) << b
wouldn't work since f
returns std::ios_base
, not std::basic_ostream
and seeing M.M.'s answer, it's probably not exactly what you wanted to achieve)
Output streams accept function with below signature, because they've provided an overload of <<
operator for this type of functions:
std::ios_base& function( std::ios_base& os )
But you're passing a std::function
which is far from that. The objects of std::function
have ()
operator to use them like functions, but they are not really functions. Therefore that streams can not accept them.
Using template way instead of std::function
is an easy fix:
template <typename F>
static void Dump( std::ostream& os, Book& b, F f )
{
os << f << b;
}