Boost.Test error output_test_stream with output statement pattern

I have a class:

class foo {
private:
    std::string data;
public:
    foo &append(const char* str, size_t n) { data.append(str,n); }

    // for debug output
    template <typename T>
    friend T& operator<< (T &out, foo const &f);

    // some other stuff
};

template <typename T>
T& operator<< (T &out, foo const &f) {
    return out << f.data;
}

I want this to work with any class that the operator provides <<.

This works great with std::cout, as in:

std::cout << fooObject;

But the following is true:

BOOST_AUTO_TEST_CASE( foo_append_and_output_operator )
{
    // fooObject is accessable here
    const char* str = "hello";
    fooObject.append(str, strlen(str));

    output_test_stream output;
    output << fooObject;

    BOOST_CHECK( output.is_equal(str) );
}

g++ tells me that:

In function ‘T& operator<<(T&, const foo&) 
    [with T = boost::test_tools::output_test_stream]’:
error: invalid initialization of reference of type
    ‘boost::test_tools::output_test_stream&’ from expression of typestd::basic_ostream<char, std::char_traits<char> >’

What's happening?

I am using Boost 1.34.1 on Ubuntu 8.04.

+3
source share
3 answers

Therefore, I think I have an explanation, but there is no solution yet. output_test_streamimplements its stream functions by subclassing wrap_stringstream. The insertion operator for this is a free function template that looks like this:

template <typename CharT, typename T>
inline basic_wrap_stringstream<CharT>&
operator<<( basic_wrap_stringstream<CharT>& targ, T const& t )
{
    targ.stream() << t;
    return targ;
}

// ... further down in the same header

typedef basic_wrap_stringstream<char>       wrap_stringstream;

output_test_stream , . . . , , , , . ?

+3

, output_test_stream std::ostream :

class foo {
    // [...]
    friend
    std::ostream& operator<< ( std::ostream &os, const foo &f );
};

std::ostream& operator<< ( std::ostream &os, const foo &f ) {
    return os << f.data;
}
+3

?

foo.append(str, strlen(str));

foo - , .

0

Source: https://habr.com/ru/post/1753283/