What legitimate reasons exist to overload the unary operator&?

前端 未结 7 1798
悲哀的现实
悲哀的现实 2020-11-28 07:39

Okay, I\'ve been inspired to do some head punching. Seems like overloading operator& leads to not a small amount of pain.

What legitimate cases exis

相关标签:
7条回答
  • 2020-11-28 08:05

    Four years later, another answer.

    Another use I have seen is when you are piggybacking off of the C++ language, but defining your own semantics. Prime example: Boost.Spirit.

    Boost.Spirit, in particular Qi for parsing, overloads operators on parsers to provide an EBNF-like syntax for specifying arbitrary parser objects. In particular, the unary & operator is overloaded to provide the And-Predicate Parser.

    And-Predicate Parser (&a)

    Description

    Syntactic predicates assert a certain conditional syntax to be satisfied before evaluating another production. Similar to semantic predicates, eps, syntactic predicates do not consume any input. The and-predicate, &a, is a positive syntactic predicate that returns a zero length match only if its predicate matches.

    Example usage:

    Basic look-ahead example: make sure that the last character is a semicolon, but don't consume it, just peek at the next character:

    test_phrase_parser("Hello ;", lit("Hello") >> &lit(';'), false);
    

    So in short, the unary & here has no relation to pointers at all; it has domain-specific semantics which apply to Qi parser objects.

    0 讨论(0)
提交回复
热议问题