0

Simple question: if unary operators have near the highest priority, then why the order of evaluation of # and ## operators is unspecified? Relevant to both C and C++.

C11 (6.10.3.2 The # operator):

The order of evaluation of # and ## operators is unspecified.

C++, N4713 (19.3.2 The # operator):

The order of evaluation of # and ## operators is unspecified.

pmor
  • 5,392
  • 4
  • 17
  • 36
  • 2
    There's `operator#`? I've only ever seen that used in macros. – super Apr 21 '21 at 15:51
  • 1
    Those aren't operators. They belong to the preprocessor. The preprocessor is what it is. (Formally, `#` and `##` are preprocessing tokens). – Pete Becker Apr 21 '21 at 15:52
  • 1
    `why the order of evaluation of # and ## operators is unspecified?` Well, standard literally says so, so it's because of that. But why do you mention unary operators and how are they connected to `#` and `##` operators? The intention is most probably so that `#` and `##` have unspecified order _with each other_, it makes no sense with other operators. – KamilCuk Apr 21 '21 at 15:53
  • 1
    @super The formal name for them are _the # operator_ and _the ## operator_. They only work inside macros indeed. If you wish to know why they are still called operators, it's because most of the C language isn't designed in consistent or rational ways. – Lundin Apr 23 '21 at 08:58

1 Answers1

7

Chapter 19 in the C++17 standard is titled "Preprocessing directives". It explains how the preprocessor works.

As the name suggests, the preprocessor is processed before the rest of C or C++'s rules. So operator precedence does not apply; these are not operators resulting in expressions. The preprocessing "operators" # and ## within a #define macro definition are not parts of the C or C++ language. They're parts of the C/C++ preprocessor; they are not "unary operators" as defined in section 8.3 of the C++17 standard.

During preprocessor evaluation and macro manipulation, there are no expressions. There is only a sequence of tokens, which the macro system defines a couple of transformation operators for (namely, # and ##). The grammar of C and C++ are not yet involved in the process.

So the question is moot: their evaluation order is unspecified because they have no relationship to regular C or C++ operators, and the standard says that their order is unspecified.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982