pair OK, struct of pair bad
Patrick Welche <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <20160616082702.GA4968@quark> |
Trying to narrow down my last post, I'm just trying to recover a pair of ints from int_ >> int_ and a space skipper. A pair of ints is compatible and works. A struct containing two ints works. A struct with a pair of ints as its only member fails. I assume this will be an issue if I want to parse +(int_ >> int_) as that will require a vector of pairs of ints? Experiments attached... Cheers, Patrick ------------------------------------------------------------------------------ What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic patterns at an interface-level. Reveals which users, apps, and protocols are consuming the most bandwidth. Provides multi-vendor support for NetFlow, J-Flow, sFlow and other flows. Make informed decisions using capacity planning reports. http://pubads.g.doubleclick.net/gampad/clk?id=1444514421&iu=/41014381 _______________________________________________ Spirit-general mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/spirit-general
struct.cpp
(text/plain, 1 KB)
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
namespace x3 = boost::spirit::x3;
#define USESTRUCT 0
#if USESTRUCT
struct result {
std::pair<int, int> both;
};
BOOST_FUSION_ADAPT_STRUCT(result,
both
)
#endif
int main()
{
std::string buf("123 456");
std::string::const_iterator iter = buf.begin();
std::string::const_iterator end = buf.end();
#if USESTRUCT
result res;
#else
std::pair<int, int> res;
#endif
x3::ascii::space_type space;
bool ok = x3::phrase_parse(iter, end, x3::int_ >> x3::int_, space, res);
if (!ok)
std::cout << "Parsing failed\n";
else if (iter != end)
std::cout << "Bit left over at the end: "
<< std::string(iter, end) << std::endl;
else
#if USESTRUCT
std::cout << "found: " << res.both.first << ' '
<< res.both.second << std::endl;
#else
std::cout << "found: " << res.first << ' '
<< res.second << std::endl;
#endif
return 0;
}
pair.cpp
(text/plain, 1 KB)
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
namespace x3 = boost::spirit::x3;
#define USEPAIR 1
struct result {
#if USEPAIR
std::pair<int, int> both;
#else
int one, two;
#endif
};
BOOST_FUSION_ADAPT_STRUCT(result,
#if USEPAIR
both
#else
one, two
#endif
)
int main()
{
std::string buf("123 456");
std::string::const_iterator iter = buf.begin();
std::string::const_iterator end = buf.end();
result res;
x3::ascii::space_type space;
bool ok = x3::phrase_parse(iter, end, x3::int_ >> x3::int_, space, res);
if (!ok)
std::cout << "Parsing failed\n";
else if (iter != end)
std::cout << "Bit left over at the end: "
<< std::string(iter, end) << std::endl;
else
#if USEPAIR
std::cout << "found: " << res.both.first << ' '
<< res.both.second << std::endl;
#else
std::cout << "found: " << res.one << ' '
<< res.two << std::endl;
#endif
return 0;
}