Name n3628, alx-0002r6 - Add operators _Minof and _Maxof Principles - Codify existing practice to address evident deficiencies. Category New operators. Author Alejandro Colomar Cc: Martin Uecker Cc: Joseph Myers History r0 (2025-03-08): - Initial draft. r1 (2025-03-08): - Fix typos. - Add link to git repository in History. r2 (2025-03-09): - Add 'See also' section. r3 (2025-03-09; n3518): - Clarify the type of the expression yielded by _Minof, _Maxof. r4 (2025-05-31; n3573): - Fix typos. r5 (2025-06-12): - wfix - ffix - Rebase on N3550. r6 (2025-06-12; n3628): - Split _Widthof to separate proposal (alx-0032). - Move the operators to new subsection. - Don't say the type of an operand type. Description Many projects need to invent their own macros for getting information about a type. Basic ones are MAXOF() and MINOF(), which get the maximum value of a type, and the minimum value of a type, respectively. Here are my own implementations: #define WIDTHOF(x) (sizeof(x) * CHAR_BIT) #define SMAXOF(T) ((T) (((((T) 1 << (WIDTHOF(T) - 2)) - 1) << 1) + 1)) #define UMAXOF(T) ((T) -1) #define IS_SIGNED(x) ((typeof(x)) -1 < 1) #define MAXOF(T) ((T) (IS_SIGNED(T) ? SMAXOF(T) : UMAXOF(T)) #define MINOF(T) ((T) ~type_max(T)) As can be seen from my implementations, they are hard to write correctly, and review. Also, now that we have bit-precise integers, one may want to get these to work with them, which would make it even harder to implement them. Also, since these are used more often in macros, we have the added problem of macros expanding arguments more than once (sometimes way more than once; sometimes resulting in macro expansions in the order of megabytes of text). If these three were keywords, true operators, the compiler would evaluate arguments exactly once, and would return the right value, even for bit-precise integers. Since these macros are typically used with type names --not expressions--, this proposal suggests only allowing a type name as the operand. It is left as a matter of QoI if compilers want to provide support for unary-expressions as with sizeof. See also Proposed wording Based on N3550. 6.4.2 Keywords ## Add _Minof, _Maxof. 6.5.4.1 Unary operators :: General @@ Syntax, p1, unary-expression alignof ( type-name ) +_Minof ( type-name ) +_Maxof ( type-name ) 6.5.4 Unary operators ## New subsection after 6.5.4.5 ("The sizeof, _Countof, and alignof operators"). +6.5.4.5+1 The _Minof and _Maxof operators + +Constraints +1 + The _Minof and _Maxof operators + shall be applied to a parenthesized integer type. + +Semantics +2 + The _Minof and _Maxof operators yield + the minimum and maximum value, respectively, + that can be represented by the operand type. + The operand is not evaluated, + and the expression is an integer constant expression + of the same type as its operand. 6.6.1 Constant expressions :: General @@ Semantics, p8 alignof expressions, +_Minof expressions, +_Maxof expressions, and floating, ... _Lengthof operator, -or alignof operator. +alignof operator, +_Minor operator, +or _Maxof operator. Update footnote 116? @@ p10 ... _Lengthof operator, -or alignof operator. +alignof operator, +_Minor operator, +or _Maxof operator. 6.7.7.3 Declarators :: Array declarators @@ Semantics, p5 ... Where a size expression is part of the operand of -an alignof operator, +an alignof operator, +a _Minof operator, +or a _Maxof operator, that expression is not evaluated. 6.9.1 External definitions :: General @@ Constraints, p3 -- part of the operand of an alignof operator which is an integer constant expression; +-- part of the operand of a _Minof operator; +-- part of the operand of a _Maxof operator; -- part of the controlling expression of a generic selection; @@ Semantics, p5 or alignof operator -that is an integer constant expression), +that is an integer constant expression, +or part of a +_Minof, +or _Maxof +operator), somewhere in the entire program A.2.2 Language syntax summary :: Lexical grammar :: Keywords Update. A.3.1 Language syntax summary :: Phrase structure grammar :: Expressions Update 6.5.4.1. J.3.16 Portability issues :: I-d behavior :: Architecture Update 7. J.6.2 P. I. :: Reserved IDs and keywords :: Rule based identifiers Update 2.