1

The following parser does not compile when I use std::optional<std::string> for values of a -lexeme[+alpha] rule: something breaks in the attribute management.

The grammar works fine if instead of std::string I use int as a base type, it also works if I use twice std::string (i.e., no std::optional). However it does not work with boost::optional either. In the real grammar there is a difference between the absence of the string, and an empty string: I want to use std::optional (or its Boost predecessor).

The full example is available on Coliru, but here are the relevant bits:

struct pair_t
{
  std::string first;
  std::optional<std::string> second;
};

BOOST_FUSION_ADAPT_STRUCT(pair_t, first, second)

const auto pair_rule = lexeme[+alpha] >> -lexeme[+alpha] >> eoi;

for (std::string i: {"ab", "ab cd"})
  {
    auto res = pair_t{};
    auto first = i.cbegin();
    auto last = i.cend();
    auto r = x3::phrase_parse(first, last, pair_rule, space, res);
    if (r && first == last)
      std::cout << i << ": " << res << '\n';
    else
      std::cout << i << ": failed\n";
  }

My compiler reports:

clang++-mp-5.0 -std=c++17   -isystem /opt/local/include/ x3-optional.cc && ./a.out 
In file included from x3-optional.cc:8:
In file included from /opt/local/include/boost/spirit/home/x3.hpp:14:
In file included from /opt/local/include/boost/spirit/home/x3/auxiliary.hpp:11:
In file included from /opt/local/include/boost/spirit/home/x3/auxiliary/any_parser.hpp:17:
/opt/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:180:9: error: no matching function
      for call to 'move_to'
        detail::move_to(std::move(src), dest
        ^~~~~~~~~~~~~~~
/opt/local/include/boost/spirit/home/x3/char/char_parser.hpp:31:29: note: in instantiation of
      function template specialization 'boost::spirit::x3::traits::move_to<const char &,
      std::__1::basic_string<char> >' requested here
                x3::traits::move_to(*first, attr);
                            ^
akim
  • 8,255
  • 3
  • 44
  • 60

0 Answers0