Compiler difference: the x3 :: variant extension requires a specific copy constrctor instance, copy assignment operator and default constructor with gcc, but not clang

I applied an example from x3 documentation ( http://ciere.com/cppnow15/x3_docs/spirit/tutorials/rexpr.html ) to parse a simple binary prefix notation grammar, and I found that I needed to define a copy command, assign copy to op and the default constructor when compiling with g ++ 6.1.1, despite the example requiring that you only need to add constructors base_typeand assign operators using using. Then I came across the fact that it compiles, as expected, with clang 3.8.1. I will write an important part of the code, and here is a complete example for g ++ with an AST printer that shows an error.

I see that g ++ seems to think that the con copy was deleted because a move constructor or move destination statement was defined, but then why does it compile with clang? Which compiler is right?

#include <iostream>
#include <string>
#include <vector>

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>

namespace x3 = boost::spirit::x3;
namespace chars = boost::spirit::x3::ascii;

namespace ast {

    struct Expression;

    struct Primary : x3::variant<
        std::string,
        x3::forward_ast<Expression>
    > {
        // GCC seems to require these three to be defined, but clang doesn't
        // if I comment them out, then it compiles on clang but not gcc
        Primary& operator=(const Primary&) = default;
        Primary(const Primary&) = default;
        Primary() = default;

        // the documentation says that only these two are necessary
        using base_type::base_type;
        using base_type::operator=;
    };

    using ArgumentList = std::vector<Primary>;

    struct Expression {
        std::string func_name;
        ArgumentList arguments;
        Expression() : func_name(), arguments() { }
    };

}

BOOST_FUSION_ADAPT_STRUCT(
    ast::Expression,
    func_name,
    arguments
)

namespace parser {
    x3::rule<class Primary,      ast::Primary     > Primary      = "Primary";
    x3::rule<class Expression,   ast::Expression  > Expression   = "Expression";

    const auto function_name = x3::lexeme[ +chars::alnum ];
    const auto number = x3::lexeme[ +chars::digit ];
    const auto Primary_def = number | Expression;
    const auto Expression_def = function_name > x3::repeat(2)[ Primary ]; // only accept binary functions for now

    BOOST_SPIRIT_DEFINE(Primary, Expression)
}

int main() {

    while (std::cin.good() == true) {

        const auto line = [&]() {
            std::string str;
            std::getline(std::cin, str);
            return str;
        }();
        auto iter_in_line = begin(line);
        const auto end_of_line = end(line);

        ast::Expression root_expr;

        const bool is_match = phrase_parse(iter_in_line, end_of_line,
            parser::Expression,
            chars::space,
            root_expr
        );
    }
}
+4
source share

Source: https://habr.com/ru/post/1650108/


All Articles