At Stackoverflow there are several Questions & Answers which are related to use {boost, std}::string_view
, e.g.:
parsing from std::string into a boost::string_view using boost::spirit::x3 with overloading x3's
move_to
namespace boost { namespace spirit { namespace x3 { namespace traits { template <typename It> void move_to(It b, It e, boost::string_view& v) { v = boost::string_view(&*b, e-b); } } } } }
parse into vector using boost::spirit::x3 where further the information about the compatibility of the attributes is given
namespace boost { namespace spirit { namespace x3 { namespace traits { template <> struct is_substitute<raw_attribute_type,boost::string_view> : boost::mpl::true_ {}; } } } }
llonesmiz wrote an example at wandbox which compiles with boost 1.64, but failed with boost 1.67 now with
opt/wandbox/boost-1.67.0/gcc-7.3.0/include/boost/spirit/home/x3/support/traits/container_traits.hpp:177:15: error: 'class boost::basic_string_view<char, std::char_traits<char> >' has no member named 'insert'
c.insert(c.end(), first, last);
~~^~~~~~
the same error I faced in my project.
The problem raises also by use of std::string
even with the explicite use of Sehe's as<> "directive", see also at wandbox:
#include <iostream>
#include <string>
#include <string_view>
namespace boost { namespace spirit { namespace x3 { namespace traits {
template <typename It>
void move_to(It b, It e, std::string_view& v)
{
v = std::string_view(&*b, e-b);
}
} } } } // namespace boost
#include <boost/spirit/home/x3.hpp>
namespace boost { namespace spirit { namespace x3 { namespace traits {
template <>
struct is_substitute<raw_attribute_type, std::string_view> : boost::mpl::true_
{};
} } } } // namespace boost
namespace parser
{
namespace x3 = boost::spirit::x3;
using x3::char_;
using x3::raw;
template<typename T>
auto as = [](auto p) { return x3::rule<struct _, T>{ "as" } = x3::as_parser(p); };
const auto str = as<std::string_view>(raw[ +~char_('_')] >> '_');
const auto str_vec = *str;
}
int main()
{
std::string input = "hello_world_";
std::vector<std::string_view> strVec;
boost::spirit::x3::parse(input.data(), input.data()+input.size(), parser::str_vec, strVec);
for(auto& x : strVec) { std::cout << x << std::endl; }
}
As far I've seen, the problem starts with boost 1.65. What has been changed and how to fix it?
Finaly, I've a question about the requirement of contiguous storage mentioned by sehe: I understand the requirements of this, but does the parser can violate this? - In my opinion the parser has to fail even on backtracking, so this can't happen by spirit. By use of the error_handler, the memory storage address which refers the string_view is at last on parse level valid. I conclude it's save to use string_view as far the references are in the scope under this condition, isn't it?