Unit Test problem with wstrings

I am using the Boost Unit Test library and have code that uses the BOOST_CHECK_EQUAL macro with std::wstring instances. I get a compilation error when I do this because there is no operator<< defined for a narrow ostream and a wide string. So, I created one as shown below, but I am still getting the exact same compilation error (using MSVC 2003). With my new operators, I am able to manually insert wide strings into a narrow ostream, but the BOOST_CHECK_EQUAL macro expansion still doesn't seem to be picking it up. Any ideas? Thanks, Sean Rohead <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #include <string> #include <iostream> std::wstring widen(const std::string& source) { std::wstring result(source.size(), wchar_t(0)); typedef std::ctype<wchar_t> ctype_t; const ctype_t& ct = std::use_facet<ctype_t>(std::locale()); ct.widen(source.data(), source.data() + source.size(), &(*result.begin())); return result; } std::string narrow(std::wstring source) { std::string result(source.size(), char(0)); typedef std::ctype<wchar_t> ctype_t; const ctype_t& ct = std::use_facet<ctype_t>(std::locale()); ct.narrow(source.data(), source.data() + source.size(), '\u00B6', &(*result.begin())); return result; } std::ostream& operator<<(std::ostream& out, const std::wstring& value) { out << narrow(value); return out; } std::wostream& operator<<(std::wostream& out, const std::string& value) { out << widen(value); return out; } #include <boost/test/unit_test.hpp> void main() { std::string message("message"); std::wstring wmessage(L"MESSAGE"); // THESE TWO LINES COMPILE AND WORK FINE std::wcout << message << std::endl; std::cout << wmessage << std::endl; // THIS LINE STILL DOES NOT COMPILE BOOST_CHECK_EQUAL(wmessage, std::wstring(L"message")); }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
------ Build started: Project: boost, Configuration: Debug Win32 ------ Compiling... main.cpp c:\lib\boost_1_32_0\boost\test\test_tools.hpp(207) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::wstring' (or there is no acceptable conversion) c:\lib\boost_1_32_0\boost\test\test_tools.hpp(206) : while compiling class-template member function 'void boost::test_tools::tt_detail::print_log_value<T>::operator ()(std::ostream &,const T &)' with [ T=std::wstring ] c:\lib\boost_1_32_0\boost\test\test_tools.hpp(251) : see reference to class template instantiation 'boost::test_tools::tt_detail::print_log_value<T>' being compiled with [ T=std::wstring ] c:\lib\boost_1_32_0\boost\test\detail\wrap_stringstream.hpp(69) : see reference to function template instantiation 'std::ostream &boost::test_tools::tt_detail::operator <<<Left>(std::ostream &,const boost::test_tools::tt_detail::print_helper<T> &)' being compiled with [ Left=std::wstring, T=std::wstring ] c:\lib\boost_1_32_0\boost\test\test_tools.hpp(438) : see reference to function template instantiation 'boost::basic_wrap_stringstream<CharT> &boost::operator <<<char,boost::test_tools::tt_detail::print_helper<T>>(boost::basic_wrap _stringstream<CharT> &,const boost::test_tools::tt_detail::print_helper<T> &)' being compiled with [ CharT=char, T=std::wstring ] c:\Documents and Settings\sean.rohead\My Documents\Visual Studio Projects\boost\main.cpp(43) : see reference to function template instantiation 'bool boost::test_tools::tt_detail::equal_and_continue_impl<std::wstring,std:: basic_string<_Elem,_Traits,_Ax>>(const Left &,const Right &,boost::wrap_stringstream &,boost::test_tools::const_string,size_t,boost::unit_test::log_level,siz e_t)' being compiled with [ _Elem=wchar_t, _Traits=std::char_traits<wchar_t>, _Ax=std::allocator<wchar_t>, Left=std::wstring, Right=std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator <wchar_t>> ] Build log was saved at "file://c:\Documents and Settings\sean.rohead\My Documents\Visual Studio Projects\boost\Debug\BuildLog.htm" boost - 1 error(s), 0 warning(s) ---------------------- Done ---------------------- Build: 0 succeeded, 1 failed, 0 skipped

I am using the Boost Unit Test library and have code that uses the BOOST_CHECK_EQUAL macro with std::wstring instances. I get a compilation error when I do this because there is no operator<< defined for a narrow ostream and a wide string. So, I created one as shown below, but I am still getting the exact same compilation error (using MSVC 2003). With my new operators, I am able to manually insert wide strings into a narrow ostream, but the BOOST_CHECK_EQUAL macro expansion still doesn't seem to be picking it up. Any ideas?
Try putting your new functions into std namespace. Gennadiy

Putting the operators into the std namespace did fix the problem. Thank you very much. "Gennadiy Rozental" <gennadiy.rozental@thomson.com> wrote in message news:dl2nkm$btk$1@sea.gmane.org...
I am using the Boost Unit Test library and have code that uses the BOOST_CHECK_EQUAL macro with std::wstring instances. I get a compilation error when I do this because there is no operator<< defined for a narrow ostream and a wide string. So, I created one as shown below, but I am still getting the exact same compilation error (using MSVC 2003). With my new operators, I am able to manually insert wide strings into a narrow ostream, but the BOOST_CHECK_EQUAL macro expansion still doesn't seem to be picking it up. Any ideas?
Try putting your new functions into std namespace.
Gennadiy

Sean Rohead wrote:
std::string narrow(std::wstring source) { std::string result(source.size(), char(0)); typedef std::ctype<wchar_t> ctype_t; const ctype_t& ct = std::use_facet<ctype_t>(std::locale()); ct.narrow(source.data(), source.data() + source.size(), '\u00B6', &(*result.begin())); return result; }
Not that this is likely to have anything to do with the error, but you mustn't use string::begin() like that. The sequence of characters is not guaranteed to be an array. If you want that guarantee, use a vector instead. Ben.
participants (3)
-
Ben Hutchings
-
Gennadiy Rozental
-
Sean Rohead