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
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
------ 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
<<
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"
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
ctype_t; const ctype_t& ct = std::use_facet (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