Search code examples
c++g++c++14clang++boost-spirit-x3

Compiler difference: Extending x3::variant requires definiton of copy constrctor, copy assign operator, and default constructor with gcc but not clang


I adapted an example from the 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 the copy con, copy assign op, and the default constructor when compiling with g++ 6.1.1, despite the example claiming that you only need to bring in the base_type's constructors & assign operators with a using. Then I came across the fact that it compiles as expected with clang 3.8.1 . I'll post the important part of the code, and here is a complete example for g++ with a AST printer that shows the error.

I see that g++ seems to think that the the copy con is deleted because a move constructor or move assign operator has been defined, but then why does it compile with clang? Which compiler is correct?

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

Solution

  • This was a GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89381, workaround also works using workaround = decltype(Primary{*static_cast<Primary const*>(0)});.