Search code examples
c++boostboost-spirit

uint_not_usable_without_attribute static assert failed when using numeric generator in karma rule


I'm unable to use a numeric boost karma generator in a karma rule, as it seems because I somehow use a wrong iterator template parameter for the rule:

#include <iostream>
#include <iterator>
#include <string>
#include <boost/spirit/include/karma.hpp>

template <typename OutputIterator>
boost::spirit::karma::rule<OutputIterator, uint_fast16_t>
        int_rule{
    boost::spirit::karma::int_
};

int main(int argc, char* argv[])
{
    std::string output;

    boost::spirit::karma::generate(std::back_inserter(output),
            int_rule<std::back_insert_iterator<std::string>>, 5);

    std::cout << "\"" << output << "\"\n";

    return 0;
}   

Fails on clang and gcc with

error: static_assert failed due to requirement '!boost::is_same

, int_<15>, unused_type>, output_iterator >, int_<15>, unused_type> >::value' "int_not_usable_without_attribute" BOOST_SPIRIT_ASSERT_FAIL(OutputIterator, int_not_usable_without_attribute, ());

When using the generator directly via boost::spirit::karma::generate(std::back_inserter(output), boost::spirit::karma::int_, 5); compiles and works, so what am I doing wrong? Which iterator instead of std::back_insert_iterator<std::string> do I have to use, in order to encapsulate a numeric boost karma generator into a karma generator rule?


Solution

  • You forgot parentheses in the rule definition. It should be uint_fast16_t(), not a plain uint_fast16_t as described in the Signature section of the rule documentation.