Paul Belanger
Paul Belanger

Reputation: 2434

'parse': is not a member of boost::spirit::x3::unused_type

I'm trying to implement a HTTP header parser using Boost Spirit, however I've become stuck on a basic subtask: extracting the HTTP version number from the first line.

Simplified code:

#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/fusion/sequence.hpp>
#include <iostream>
#include <string>

namespace parser
{
    namespace ast
    {
        struct version_number
        {
            int major = 0;
            int minor = 0;
        };
    }

    using boost::fusion::operator<<;
}
BOOST_FUSION_ADAPT_STRUCT(
    parser::ast::version_number,
    (int, major)
    (int, minor)
)

namespace parser
{
    using namespace boost::spirit::x3;

    class version_number_id;
    rule<version_number_id, ast::version_number> version_number = "version_number";
    auto const version_number_def = eps
        >> int_
        >> "."
        >> int_
        ;
    BOOST_SPIRIT_DEFINE(rtsp_request_line)
}

int main()
{
    using namespace std;
    string input = "4.332";


    parser::ast::version_number vn;
    auto res = boost::spirit::x3::parse(begin(input), end(input), parser::version_number, vn);
    if (res)
    {
        cout << "Success" << endl;
    }
    else
    {
        cout << "Failure" << endl;
    }
}

However even with this simple example I've run into compilation errors using Boost v1.64 and MSVC++ 14.1. Compiler output is as follows:

1>------ Build started: Project: spirit_test, Configuration: Debug Win32 ------
1>main2.cpp
1>c:\devlib\boost_1_64_0\include\boost\spirit\home\x3\nonterminal\rule.hpp(36): error C2338: BOOST_SPIRIT_DEFINE undefined for this rule.
1>c:\devlib\boost_1_64_0\include\boost\spirit\home\x3\nonterminal\rule.hpp(116): note: see reference to function template instantiation 'boost::spirit::x3::detail::default_parse_rule_result boost::spirit::x3::parse_rule<parser::version_number_id,parser::ast::version_number,Iterator,Context,Attribute_>(boost::spirit::x3::rule<parser::version_number_id,parser::ast::version_number,false>,Iterator &,const Iterator &,const Context &,ActualAttribute &)' being compiled
1>        with
1>        [
1>            Iterator=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,
1>            Context=boost::spirit::x3::unused_type,
1>            Attribute_=parser::ast::version_number,
1>            ActualAttribute=parser::ast::version_number
1>        ]
1>c:\devlib\boost_1_64_0\include\boost\spirit\home\x3\core\parse.hpp(35): note: see reference to function template instantiation 'bool boost::spirit::x3::rule<parser::version_number_id,parser::ast::version_number,false>::parse<Iterator,boost::spirit::x3::unused_type,Attribute>(Iterator &,const Iterator &,const Context &,boost::spirit::x3::unused_type,Attribute_ &) const' being compiled
1>        with
1>        [
1>            Iterator=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,
1>            Attribute=parser::ast::version_number,
1>            Context=boost::spirit::x3::unused_type,
1>            Attribute_=parser::ast::version_number
1>        ]
1>c:\devlib\boost_1_64_0\include\boost\spirit\home\x3\core\parse.hpp(35): note: see reference to function template instantiation 'bool boost::spirit::x3::rule<parser::version_number_id,parser::ast::version_number,false>::parse<Iterator,boost::spirit::x3::unused_type,Attribute>(Iterator &,const Iterator &,const Context &,boost::spirit::x3::unused_type,Attribute_ &) const' being compiled
1>        with
1>        [
1>            Iterator=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,
1>            Attribute=parser::ast::version_number,
1>            Context=boost::spirit::x3::unused_type,
1>            Attribute_=parser::ast::version_number
1>        ]
1>c:\devlib\boost_1_64_0\include\boost\spirit\home\x3\core\parse.hpp(60): note: see reference to function template instantiation 'bool boost::spirit::x3::parse_main<Iterator,Parser,Attribute>(Iterator &,Iterator,const Parser &,Attribute &)' being compiled
1>        with
1>        [
1>            Iterator=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,
1>            Parser=boost::spirit::x3::rule<parser::version_number_id,parser::ast::version_number,false>,
1>            Attribute=parser::ast::version_number
1>        ]
1>d:\documents\visual studio 2017\projects\spirit_test\spirit_test\main2.cpp(49): note: see reference to function template instantiation 'bool boost::spirit::x3::parse<std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,boost::spirit::x3::rule<parser::version_number_id,parser::ast::version_number,false>,parser::ast::version_number>(const Iterator &,Iterator,const Parser &,Attribute &)' being compiled
1>        with
1>        [
1>            Iterator=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,
1>            Parser=boost::spirit::x3::rule<parser::version_number_id,parser::ast::version_number,false>,
1>            Attribute=parser::ast::version_number
1>        ]
1>c:\devlib\boost_1_64_0\include\boost\spirit\home\x3\nonterminal\rule.hpp(37): error C2039: 'parse': is not a member of 'boost::spirit::x3::unused_type'
1>c:\devlib\boost_1_64_0\include\boost\spirit\home\x3\support\traits\attribute_category.hpp(22): note: see declaration of 'boost::spirit::x3::unused_type'
1>Done building project "spirit_test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========

I notice that in the debug messages, the Context=boost::spirit::x3::unused_type line appearing, and I known that unused_type is a tag class used to indicate that the rule does not populate an attribute. I don't understand where that tag is coming from in this example, however.

If I modify the code by using an inline rule with no rule<> definition, and pass a std::pair<int, int> in as the attribute instead, the code compiles and runs fine.

What's going on here, and where did I go wrong with this simple example?

Upvotes: 1

Views: 563

Answers (2)

ildjarn
ildjarn

Reputation: 62975

In your code you have BOOST_SPIRIT_DEFINE(rtsp_request_line), but the the rule's actual name is version_number. Once this typo is fixed your code compiles cleanly for me with Boost 1.65.1 and VC++ 2017 Update 3 (compiler v19.11.25508).

Upvotes: 2

sehe
sehe

Reputation: 393029

It's simply a problem with MSVC. MSVC compiler is not supported for now.

It's not a problem on other compilers: http://coliru.stacked-crooked.com/a/aba5282f5cb7bb02

Source on supported compilers: http://boost-spirit.com/home/2015/05/16/spirit-3-0-0/

Interestingly, the only online MSVC compiler I know of does underline the diagnosis: MSVC is just not supported (http://rextester.com/ZSEF55595), though it gives a different error - possibly due to Boost 1.60.0 there.

On GCC/Clang Boost 1.60.0 is not a hurdle though:

Upvotes: 0

Related Questions