1

im confused with boost Spirit X3 and string_ref class. How to construct a string_ref object. The console prints somethink like this :

N5boost16basic_string_refIcSt11char_traitsIcEEE / N5boost14iterator_rangeIN9__gnu_cxx17__normal_iteratorIPcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEE

I dont know how to translate it to "normal type name" but it seems to me that i need to create the string_ref from a boost iterator range ?

 auto f = [](auto& ctx)
    { 
        auto val  = _val(ctx);
        auto attr = _attr(ctx);

        std::cout << typeid(val).name() << " / " << typeid(attr).name() << "\n";
        // _val(ctx) = boost::string_ref( attr.c_str(), 1 ); //phx::construct<boost::string_ref>( _where(ctx).begin(), _attr(ctx).size() );
     };

And the rule is :

  auto const quote  = rule<struct quote, Type>{"quote"} 
                    =    lit('"')
                     >> raw[*~char_('"')][ f ]
                     >> lit('"');
Roby
  • 2,011
  • 4
  • 28
  • 55
  • ["normal type name"](http://coliru.stacked-crooked.com/a/07afc74fd304649d). – llonesmiz Feb 24 '16 at 13:21
  • 1
    Be careful creating a `string_ref` from an `iterator_range`. A `string_ref` requires the characters to be contiguous, while `iterator_range` does not. You have to assure that yourself. – Marshall Clow Feb 24 '16 at 15:43

1 Answers1

2

After the comment from cv_and_he I'm using the following lambda as semantic action

auto f = [](auto& ctx)
{ 
    auto& attr = _attr(ctx);
    _val(ctx) = boost::string_ref( &attr[0], attr.size() );
};
Roby
  • 2,011
  • 4
  • 28
  • 55
  • 1
    Not too sure that makes sense. After all now you have a dangling ref to a temporary string or vector, in 99% of the cases. – sehe Feb 25 '16 at 21:16