Opened 10 years ago

Closed 4 years ago

Last modified 4 years ago

#7299 closed Bugs (fixed)

Spirit Karma static assert with unicode enabled on Win32 and Win64

Reported by: Dave Bailey <David.Bailey@…> Owned by: Hartmut Kaiser
Milestone: To Be Determined Component: spirit
Version: Boost 1.50.0 Severity: Problem
Keywords: spirit karma unicode Cc:

Description

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;
}

Attachments (1)

output_iterator.hpp.patch (2.1 KB ) - added by Dave Bailey <David.Bailey@…> 10 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 by Joel de Guzman, 10 years ago

Owner: changed from Joel de Guzman to Hartmut Kaiser

by Dave Bailey <David.Bailey@…>, 10 years ago

Attachment: output_iterator.hpp.patch added

comment:2 by Dave Bailey <David.Bailey@…>, 10 years ago

I've attached a patch that solves the problem. It's not particularly elegant but seems to do the job.

comment:4 by Joel de Guzman, 4 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.