Re: fusion pair issue?
Seth <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <[email protected]> |
On 27-03-17 17:34, Patrick Welche wrote:
> Given the trivial working attached code, I do the following:
>
> --- works.cpp 2017-03-27 17:25:13.008492450 +0200
> +++ fails.cpp 2016-06-14 13:17:12.683679158 +0200
> @@ -2,16 +2,17 @@
>
> #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;
>
> struct result
> {
> - std::string first, second;
> + std::pair<std::string, std::string> both;
> };
>
> BOOST_FUSION_ADAPT_STRUCT(result,
> - first, second
> + both
> )
>
> int main()
> @@ -27,8 +28,8 @@
> std::cout << "Bit left over at the end: "
> << std::string(iter, end) << std::endl;
> else
> - std::cout << "found: " << res.first << ' '
> - << res.second << std::endl;
> + std::cout << "found: " << res.both.first << ' '
> + << res.both.second << std::endl;
>
> return 0;
> }
>
>
> and get a compiler error. What have I forgotten to change?
>
> (Hoping to use the automatic semantic action feature of x3. I'm sure I
> could write a lot more code and have it work, but I don't see why a priori
> the above isn't sufficient...)
Equivalent would be
#define BOOST_SPIRIT_X3_DEBUG
#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;
using result = std::pair<std::string, std::string>;
See http://coliru.stacked-crooked.com/a/5e266b02bf68272a
Alternatively use:
struct result : std::pair<std::string, std::string> {};
BOOST_FUSION_ADAPT_STRUCT(result, first, second)
See http://coliru.stacked-crooked.com/a/674c614ed7f75e70
Or
result res;
bool ok = x3::parse(iter, end, +x3::alnum >> ' ' >> +x3::alnum,
res.both);
See http://coliru.stacked-crooked.com/a/2a847ad1037640f3
Or even more mixages:
struct result
{
std::pair<std::string, std::string> both;
};
BOOST_FUSION_ADAPT_STRUCT(result, both.first, both.second)
See http://coliru.stacked-crooked.com/a/96ddef3f1dcba167
Last one, in case you really need to force automatic attribute
propagation without any Fusion sequence unpacking hints:
result res;
auto const p = x3::rule<struct _, decltype(result::both)> {}
= +x3::alnum >> ' ' >> +x3::alnum;
bool ok = x3::parse(iter, end, p, res);
See http://coliru.stacked-crooked.com/a/020b2510b47aa467
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general