I am beginning with Boost Spirit x3 parsing library - and I'm very excited about it.
One thing unclear to me is when and why one should use x3::lit.
From what I understood, it's because we can not expect an expression like ']' >> double_ to be interpreted as intended within C++ syntactic rules.
But my interpretation seems incorrect, since the official documentation displays numerous examples of simple strings followed by the >> operator, and others instances where parenthesis and/or brackets have to be specified as lit(']')
What am I missing?
You can not generally expect
'[' >> xto see the X3 expression overload.Overload resolution depends on the types of both operands. Since
'[' is char, there cannot be user-defined overloads. So in this case, only ifxis already an x3 parser expression the x3 overload ofoperator>>will be found.In generic code, if both operands are unknown you should use
x3::as_parserinstead ofx3::litbecause it will leave other types of expressions intact, but promotes string literals like you would expect:The mechanism for overload resolution at play in e.g.
'x' >> x3::double_is ADL: https://en.cppreference.com/w/cpp/language/adl (Incidentally it's the same mechanism that makesstd::cout << "Hello world\n"find thestd::operator>>overload)