Name n3518, alx-0002r2 - Add operators _Widthof, _Minof, _Maxof Principles - Codify existing practice to address evident deficiencies. Category New operators. Author Alejandro Colomar Cc: Martin Uecker 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. - Present as n3518. Description Many projects need to invent their own macros for getting information about a type. Basic ones are WIDTHOF(), MAXOF(), and MINOF(), which get the width of a type, 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 N3467. 6.4.2 Keywords Add _Widthof, _Minof, _Maxof. 6.5.4.1 Unary operators :: General @@ Syntax, p1, unary-expression alignof ( type-name ) +_Widthof ( type-name ) +_Minof ( type-name ) +_Maxof ( type-name ) 6.5.4.5 :: The sizeof, _Lengthof, and alignof operators @@ Title -The sizeof, _Lengthof, and alignof operators +The sizeof, _Lengthof, alignof, _Widthof, _Minof, and _Minof operators @@ Constraints, append to p1 +The _Widthof, _Minof, and _Maxof operators +shall be applied to a parenthesized integer type. @@ Semantics, new p after p5 +The _Widthof operator yields the width (in bits) of its operand type. +The operand is not evaluated, +and the result is an integer constant expression. @@ Semantics, new p after p5 +The _Minof and _Maxof operators yield +the minimum and maximum value, respectively, +that can be represented by the type of the operand type. +The operand is not evaluated, +and the result is an integer constant expression +of the same type as its operand type. 6.6.1 Constant expressions :: General @@ Semantics, p8 alignof expressions, +_Widthof expressions, +_Minof expressions, +_Maxof expressions, and floating, ... _Lengthof operator, -or alignof operator. +alignof operator, +_Widthof operator, +_Minor operator, +or _Maxof operator. Update footnote 115? @@ p10 whose results are integer constant expressions, -and alignof expressions. +alignof expressions, +_Widthof expressions, +_Minof expressions, +and _Maxof expressions. ... _Lengthof operator, -or alignof operator. +alignof operator, +_Widthof 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 _Widthof 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 whose result is an integer constant expression; +-- part of the operand of a _Widthof operator; +-- 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 -whose result is an integer constant expression), +whose result is an integer constant expression, +or part of a +_Widthof, +_Minof, +_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.