Добрый день.
Есть следующий код:
std::ostream& operator<<( std::ostream& stream, const std::wstring& instance )
{
return stream << "hi!";
}
namespace lib
{
template< typename CharT >
class basic_wrap_stringstream
{
typedef std::basic_ostringstream<CharT> wrapped_stream;
public:
wrapped_stream& stream();
private:
wrapped_stream m_stream;
};
template <typename CharT, typename T>
inline basic_wrap_stringstream<CharT>& operator<<( basic_wrap_stringstream<CharT>& targ, T const& t )
{
targ.stream() << t;
return targ;
}
template<typename T>
struct print_log_value
{
void operator()( std::ostream& ostr, T const& t )
{
// error C2679: binary '<<' : no operator found which takes a right-hand operand of type
// 'const _STL::wstring' (or there is no acceptable conversion)
ostr << t;
}
};
} // namespace lib
using namespace lib;
int main()
{
print_log_value< std::wstring > plv;
plv( std::cout, std::wstring() );
}
В выделенной строке происходит ошибка компиляции. Если operator<< внести в пространство имен lib, все нормально компиляется.
Голова уже совсем не варит, поэтому два вопроса:
1. Почему происходит ошибка?
2. Возможно ли обойти ее, не внося клиентский оператор сдвига в namespace lib?
Спасибо