सबसे पहले, आत्मा V2 पर स्विच करें - जिसने शास्त्रीय भावनाओं को कई वर्षों से हटा दिया है।
दूसरा, आपको यह सुनिश्चित करना होगा कि एक int को प्राथमिकता दी जाए। डिफ़ॉल्ट रूप से, एक डबल किसी भी पूर्णांक समान रूप से अच्छी तरह से पार्स कर सकते हैं, तो आप के बजाय strict_real_policies
उपयोग करने की आवश्यकता:
real_parser<double, strict_real_policies<double>> strict_double;
अब आप बस कह सकते हैं
number = strict_double | int_;
देखें
परीक्षण कार्यक्रम देखें Live on Coliru
#include <boost/spirit/include/qi.hpp>
using namespace boost::spirit::qi;
using A = boost::variant<int, double>;
static real_parser<double, strict_real_policies<double>> const strict_double;
A parse(std::string const& s)
{
typedef std::string::const_iterator It;
It f(begin(s)), l(end(s));
static rule<It, A()> const p = strict_double | int_;
A a;
assert(parse(f,l,p,a));
return a;
}
int main()
{
assert(0 == parse("42").which());
assert(0 == parse("-42").which());
assert(0 == parse("+42").which());
assert(1 == parse("42.").which());
assert(1 == parse("0.").which());
assert(1 == parse(".0").which());
assert(1 == parse("0.0").which());
assert(1 == parse("1e1").which());
assert(1 == parse("1e+1").which());
assert(1 == parse("1e-1").which());
assert(1 == parse("-1e1").which());
assert(1 == parse("-1e+1").which());
assert(1 == parse("-1e-1").which());
}
ऐसा नहीं है कि मैं सवाल का परिदृश्य समझ में नहीं आता, लेकिन नहीं 'संख्या = double_ हैं संभव है | int_' बस काम? –
@sehe, बहुत बहुत धन्यवाद, यह एक आकर्षण की तरह काम करता है। –
@llonesmiz हां, real_parser>() | int_ भी ठीक काम करता है। –