I want to wrap C++ streams in a template class such that all << operations that are defined for the streams are already available for the wrapper class.
Can you change the following code such that it compiles without altering the overall intention too much?
#include <iostream>
class Foo
{
private:
std::ostream& os;
public:
explicit Foo( std::ostream& os ) : os( os ) {};
template<class T>
Foo& operator<<( const T& t )
{
os << t << '!';
return *this;
}
};
int main()
{
Foo( std::cout ) << "test" << '\n'; // works fine
Foo( std::cout ) << "test" << std::endl; // compilation error
return 0;
}
I assumed that std::endl has some type and hence is 'caught' by the templated method.