But now I feel like I want it to be able to work with both ostream and wostream, and this is where my issue arises. I could always just write another class called WFoo that replaces the string with wstring and ostream with wostream, but that seems like a waste of time and typing since it will be the exact code just with different types.
So then I thought I could template the class like this:
#include <string>
#include <iostream>
template <typename charT>
class BasicFoo {
private:
std::basic_string<charT> n_;
public:
BasicFoo( const std::basic_string<charT>& n ) : n_( n ) {}
const std::basic_string<charT>& n() const { return n_; }
};
/*
***Here is the problem****
basic_ostream<charT>& operator<<( basic_ostream<charT>& os, const BasicFoo<charT>& foo ) {
return os << foo.n();
}*/
typedef BasicFoo<char> AFoo;
typedef BasicFoo<wchar_t> WFoo;
Since operator<< isn't a member of the class, it doesn't know what the template parameter is, so I still have a problem of how to accept ostream and wostream. Any ideas on how to implement this would be greatly appreciated.