boost :: spirit :: qi :: double_和boost :: spirit :: qi :: int_

时间:2022-09-08 23:52:27

How to parse a string which may contain either double or int depending on whether the dot is set. E.g. 6.0 is of double type and 6 is of int type. The rule would be

如何解析可能包含double或int的字符串,具体取决于是否设置了点。例如。 6.0是双重类型,6是int类型。规则是

rule<it,boost::variant<int,double>,skipper> r = qi::double_|qi::int_;

However, a stream will be fed by double as for all numbers in this case.

但是,在这种情况下,对于所有数字,流将以双精度进给。

2 个解决方案

#1


4  

In addition to the pragmatic approach1 given by interjay, have a look at real_parser_policies:

除了interjay给出的实用方法1之外,还要看一下real_parser_policies:

real_parser<double,strict_real_policies<double>>() | int_

would be equally good.

同样好。


1 which I sometimes use myself (you should be able to find an answer doing this on SO). Note, however that there are problems when the input is e.g. 123e-5 (which would parse an int, leaving e-5 unparsed).

1我有时会自己使用(你应该能够在SO上找到答案)。但是请注意,输入是例如存在问题。 123e-5(将解析一个int,留下e-5未解析)。

#2


0  

I think this should work:

我认为这应该有效:

(int_ >> !lit('.')) | double_

It will match an integer only if it isn't followed by a dot. Otherwise, it will match a double.

只有当后面没有点时,它才会匹配整数。否则,它将匹配一个双。

#1


4  

In addition to the pragmatic approach1 given by interjay, have a look at real_parser_policies:

除了interjay给出的实用方法1之外,还要看一下real_parser_policies:

real_parser<double,strict_real_policies<double>>() | int_

would be equally good.

同样好。


1 which I sometimes use myself (you should be able to find an answer doing this on SO). Note, however that there are problems when the input is e.g. 123e-5 (which would parse an int, leaving e-5 unparsed).

1我有时会自己使用(你应该能够在SO上找到答案)。但是请注意,输入是例如存在问题。 123e-5(将解析一个int,留下e-5未解析)。

#2


0  

I think this should work:

我认为这应该有效:

(int_ >> !lit('.')) | double_

It will match an integer only if it isn't followed by a dot. Otherwise, it will match a double.

只有当后面没有点时,它才会匹配整数。否则,它将匹配一个双。