1

I have simple parser with some optional parts. And I've noticed what if I use a custom struct as attribute than omitted values are zero-initialized.

struct face_triplet
{
    int v, vt, vn;
};
BOOST_FUSION_ADAPT_STRUCT(
    face_triplet,
    (int, v)
    (int, vt)
    (int, vn)
)

void test( std::string_view line )
{
    namespace x3 = boost::spirit::x3;

    std::vector<face_triplet> f;

    auto line_begin = line.begin();
    if( x3::phrase_parse( line_begin, line.end(), 
        'f' >> +( x3::lexeme[ x3::int_ >> -( ( '/' >> -x3::int_ >> -( '/' >> x3::int_ ) ) ) ] ), x3::lit(' ')|'\t', f ) )
    {
        std::cout << "f ";
        for( auto && v : f )
            std::cout << v.v << '/' << v.vt << '/' << v.vn << ' ';
        std::cout << '\n';
    }
}

For example, parsing f 1//100 2//200 3//300 fills f with 1/0/100 2/0/200 3/0/300

demo on wandbox

Can I rely on this behavior? Or is it just a coincidence which can break with future versions?

Ryhor Spivak
  • 253
  • 1
  • 8

0 Answers0