5

Unary operators are typically parsed with higher precedence than binary operators, and when scanning left-to-right a prefix operator will be found first. So why is the order of evaluation of stringize (#) operators unspecified with respect to concatenation (##) operators? Does order of evaluation mean something different from precedence in the context of [cpp.stringize] §16.3.2?

(The preprocessor has no side effects, so technically it has no such thing as order of evaluation.)

And how does the text "replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument" jibe with other precedence, given that any alternative would stringize the result of concatenation, not the argument per se?

Does any implementation do something funny, or could the order of evaluation text be safely removed and replaced with "The # operator has higher precedence than the ## operator"?

This question is cross-posted to the std-discussion mailing list, but please prefer to reply here.

Note: I plan to draft an official proposal to revise this part of the C++ specification, so please share your knowledge! The underlying motivation is to make the preprocessor more deterministic.

2
  • First of all, "precedence" and "order of evaluation" are completely orthogonal concepts... Dec 30, 2013 at 6:35
  • @R.. A grain of salt is needed because "order of evaluation," the exact term used in the Standard, is meaningless in the literal context. I'd welcome any enlightenment as to the origin of this part of the spec, since AFAIK no historical preprocessor has ever had side effects. Dec 30, 2013 at 7:50

1 Answer 1

3

I hope to see another answer, but following @R's comment I'm leaning toward the opinion that the standard means exactly what it says. Due to absence of side effects, the order of evaluation has no effect in the preprocessor, so it shouldn't be specified. Precedence and associativity of # and ## should be specified, but are omitted from the standard.

Each of the terms associativity, precedence, and order of evaluation has a mutually exclusive meaning and the standard should be above confuting them.

Edit: I've formally proposed to resolve this. (See section 5.1.)

7
  • 'has no effect' is definitely not true. Clearly the order of evaluation has effect in the preprocessor. Consider #define e(a,b) # a ## b if a is glued to b and then stringized, the behavior is defined. If a gets stringized THEN glued to b, the results is not a valid preprocessing token and the behavior is undefined. I think most would agree that has serious repercussions.
    – Wiz
    Jan 5, 2014 at 22:36
  • @Wiz You are referring to precedence, not order of evaluation. Anyway, the definition of the stringize operator essentially precludes the paste from happening "first". I've edited the answer now to be a bit more clear. Jan 6, 2014 at 2:52
  • You are right. But that leads me to believe the standard meant precedence instead of order when it said C11 §6.10.3.2 p3 The order of evaluation of # and ## operators is unspecified. Since the order of macro evaluation itself (expansions) wouldn't change anything. However the precedence does. BTW, where did you get that the stringize operator can't happen first? (That example happens to come from an aerospace safe coding practices booklet.)
    – Wiz
    Jan 6, 2014 at 20:33
  • @Wiz Can you give me a reference to that book? I'm writing a formal ISO proposal to revise the preprocessor spec and that would be good <s>ammunition</s> evidence that the lacking quality of the current spec has real consequences. The concatenation result cannot be stringized because the stringize operator applies specifically to the following macro parameter. There is no way to construe a ## b as a macro parameter. Also, a result token "a"b is well-formed if b is empty, or in C++11 where it forms a ud-string-literal. Jan 7, 2014 at 2:47
  • @Wiz Ping… can you please provide a reference to the aerospace coding booklet? Even if it's heavily copyrighted or protected somehow, a quote and reference would be invaluable to me. Too many industrial guidelines are strangely "closed"… Jan 19, 2014 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.