boost::variant und der ostream operator?



  • Hallo nochmal! 🙂

    Um in meiner boost::spirit grammar vernuenftig die debug() funktion benutzen zu koennen muss mein synthetisiertes Attribut printable sein.
    Nun habe ich versucht einen ostream operator fuer mein Attribut zu erstellen, aber ich renne von einem Compilerfehler in den naechsten 😕

    Vielleicht weiss jemand Rat?

    struct AstNode {
        typedef boost::variant<
                        Nil,  // should not happen, only for initialization  
                        unsigned int,
                        double,
                        boost::recursive_wrapper<AstNode>,
                        boost::recursive_wrapper<UnaryOperator>,
                        boost::recursive_wrapper<BinaryOperator>
                    >
                type;
    
        AstNode()
            : expression(Nil()) {}
    
        template <typename ExpressionType>
        AstNode(ExpressionType const& expr)
            : expression(expr) {}
    
        type    expression;
    };
    
    class output_visitor : public boost::static_visitor<std::ostream&>
    {
        std::ostream& os;
    public:
        output_visitor(std::ostream& os_):os(os_) {};
    
        result_type operator()(boost::spirit::qi::info::nil) const {
            os << "nil";
            return os;
        }
        result_type operator()(new_grammar::Nil& arg) const {
            os << "Nil";
            return os;
        }
        result_type operator()(unsigned int& arg) const {
            os << arg;
            return os;
        }
        result_type operator()(double& arg) const {
            os << arg;
            return os;
        }
        result_type operator()(new_grammar::AstNode& arg) const {
            os << "AstNode";
            return os;
        }
    };
    
    std::ostream& operator<<( std::ostream& os, new_grammar::AstNode const& node )
    {
        output_visitor out_v(os);
        boost::apply_visitor(out_v, node.expression);
        return os;
    }
    

    Der Fehler lautet:

    1>x:\boost\variant\variant.hpp(832): error C2664: 'std::basic_ostream<_Elem,_Traits> &output_visitor::operator ()(new_grammar::AstNode &) const' : cannot convert parameter 1 from 'const T0' to 'new_grammar::AstNode &'
    1>          with
    1>          [
    1>              _Elem=char,
    1>              _Traits=std::char_traits<char>
    1>          ]
    1>          x:\boost\variant\detail\visitation_impl.hpp(130) : see reference to function template instantiation 'std::basic_ostream<_Elem,_Traits> &boost::detail::variant::invoke_visitor<Visitor>::internal_visit<const T>(T &,int)' being compiled
    1>          with
    1>          [
    1>              _Elem=char,
    1>              _Traits=std::char_traits<char>,
    1>              Visitor=output_visitor,
    1>              T=T0
    1>          ]
    

Log in to reply