Spirit Karma static assert with unicode enabled on Win32 and Win64
The following code causes a static assert in boost/spirit/home/karma/detail/output_iterator.hpp. The problem is that buffer_sink stores charactes in a std::basic_string<wchar_t>, but when BOOST_SPIRIT_UNICODE is defined, the output character size is 32-bit. On Windows, wchar_t is 16-bit, which will always hit the static assert. On Linux, wchar_t is 32-bit so we don't have the issue.
#ifndef BOOST_SPIRIT_UNICODE
#define BOOST_SPIRIT_UNICODE
#endif
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/qi.hpp>
#include <boost/spirit/home/karma.hpp>
#include <string>
namespace karma = boost::spirit::karma;
template<typename OutputIterator>
struct unicode_char_
: public karma::grammar<OutputIterator, ::boost::spirit::char_encoding::unicode::char_type()>
{
unicode_char_() : base_type(thechar)
{
using karma::unicode::char_;
thechar = char_;
}
karma::rule<OutputIterator, ::boost::spirit::char_encoding::unicode::char_type()> thechar;
};
int main()
{
typedef std::basic_string<boost::spirit::char_encoding::unicode::char_type> unicode_string;
typedef std::back_insert_iterator<unicode_string> unicode_back_insert_iterator_type;
unicode_string input;
unicode_string output;
unicode_back_insert_iterator_type insert_iter(output);
unicode_char_<unicode_back_insert_iterator_type> unichar;
karma::generate(insert_iter,unichar,input);
return 0;
}
I've attached a patch that solves the problem. It's not particularly elegant but seems to do the job.