Operador de división de implementación

I'm writing a simple calculator using boost spirit.

I want the division operator to throw an exception if a zero denominator is encountered.

Estoy pensando en la línea de

 term =
     factor                      [qi::_val = qi::_1]
     >> *(('*' >> factor         [qi::_val *= qi::_1])
         ('/' >> factor  
        [qi::_val = boost::phoenix::if_else(
            qi::_val / qi::_1, 
            /*ToDo some exception actor here*/)

However, for this to make sense, the exception actor not only needs to lazy-throw "division by zero" but it also has to have an implicit return type compatible with qi::_val. That's where I'm stuck. Is there something in phoenix that I can use here or do I need to bind to a hand-coded function?

preguntado el 12 de junio de 14 a las 11:06

1 Respuestas

Boost Phoenix allows you to Grupo statements. Parentheses are used to do that. This, along with boost::phoenix::throw_ allows you to write

(boost::phoenix::throw_("division by zero"), qi::_1)

en tu ToDo bloquear. qi::_1 will not be evaluated but (i) you know it would evaluate to 0 as it will have "failed" the if_else and (ii) it has the correct type.

Respondido el 12 de junio de 14 a las 11:06

Thanks Batsheba. You and sehe seem to be good at this stuff. I find the boost docs hard to read. - P45 Imminent

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.