The following example compiles fine, but I cannot figure out how to separate the declaration and definition of the <<() operator in this particular case.
Every time I try to share a different definition, a problem occurs and gcc complains that the <<() operator should only take one argument.
#include <iostream>
template <typename T>
class Test {
public:
Test(const T& value) : value_(value) {}
template <typename STREAM>
friend STREAM& operator<<(STREAM& os, const Test<T>& rhs) {
os << rhs.value_;
return os;
}
private:
T value_;
};
int main() {
std::cout << Test<int>(5) << std::endl;
}
The operator <(lt) must have a free first parameter for working with various types of output streams (std :: cout, std :: wcout or boost :: asio :: ip :: tcp :: iostream). The second parameter must be bound to a specialized versions of the surrounding class.
Test<int> x;
some_other_class y;
std::cout << x;
boost::asio::ip::tcp::iostream << x;
std::cout << y;
boost::asio::ip::tcp::iostream << y;
, , , , , , .