2
4

2 回答 2

3

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_ostreamand seeing M.M.'s answer, it's probably not exactly what you wanted to achieve)

于 2013-10-09T13:41:08.287 回答
1

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;
}
于 2013-10-09T13:40:56.177 回答