Title: Literal Suffixes for size_t
H1: Literal Suffixes for size_t
Shortname: 2998
Revision: 1
Status: P
!Previous Revisions: N2961 (r0)
Date: 2022-06-17
Group: WG14
!Proposal Category: Feature Request
!Target: C2y/C3a
Editor: JeanHeyd Meneide (thephd.dev), phdofthehouse@gmail.com
URL: https://thephd.dev/_vendor/future_cxx/papers/C%20-%20Literal%20Suffixes%20for%20size_t.html
!Latest: https://thephd.dev/_vendor/future_cxx/papers/C - Literal Suffixes for size_t.html
!Paper Source: github.com/ThePhD/future_cxx
Issue Tracking: GitHub https://github.com/ThePhD/future_cxx/issues
Metadata Order: Previous Revisions, Editor, Latest, Paper Source, Issue Tracking, Proposal Category, Target, Project
Markup Shorthands: markdown yes
Toggle Diffs: no
Abstract: C++ has suffixed literals for size_t and its signed counterpart. This paper gives C the same ability.
path: resources/css/bikeshed-wording.html
# Changelog # {#changelog} ## Revision 1 - June 17th, 2022 ## {#changelog-r1} - Add constraint to prevent `size_t` literals from becoming extended integer types (will be a proper Constraint Violation). ## Revision 0 - April 12th, 2022 ## {#changelog-r0} - Initial Release 🎉 - Targeting C2y/C3a (after C23). # Design # {#design} This is a (minor) compatibility issue with literal suffixes that may be potentially used in shared code in the future. C++ adopted suffixed literals for size_t in [[p0330]]. The design is as follows: ```cpp #include size_t ulit_val = 0zu; ``` The `u`, as with current literals, can be placed on either the left or the right of the `z` suffix to make it unsigned. For symmetry with existing suffix literal rules, it also has a signed variant. That type is the "signed integer type of size_t", which normally resolves to `ptrdiff_t`: ```cpp #include ptrdiff_t lit_val = 0z; ``` The signed variant lacks the `u` as a piece of the `z` suffix. This also matches the way `printf` adjusts specific codes to display `size_t` or `ptrdiff_t`-sized variables. The design is simple and, thankfully, compatible with C++. It also provides a way to avoid signed comparison warnings in compilers which implement more strict comparisons checks, e.g., when comparing a `size_t` value against some fixed constant value. ## Even After Compatibility, Do We Really Need This? ## {#design-really} Yes. The trip to put this paper into C++ was an extremely long one and came with a ton of reasons. All of it can be seen in [[p0330]] and 90% of that reasoning applies to C, especially in the face of `_Generic`. # Wording # {#wording} Wording is relative to [[N2912]]. ## Intent ## {#wording-intent} The goal of this wording is to provide: - a literal suffix for both the signed (`ptrdiff_t`) or unsigned (`size_t`) variant of the type of the expression "`sizeof(0)`"; and, - use the suffix `z` in either the form `uz` or `zu` to produce a `size_t`. ## Specification ## {#wording-specification} ### Add two new grammar productions to §6.4.4.1 Integer constants¸ Syntax, ¶1 ### {#wording-specification-6.4.4.1p1}
6.4.4.1 Integer constants
Syntax
…
integer-suffix:
unsigned-suffix long-suffixopt
unsigned-suffix long-long-suffix
unsigned-suffix size-suffix
long-suffix unsigned-suffixopt
long-long-suffix unsigned-suffixopt
size-suffix unsigned-suffixopt
…
long-long-suffix: one of
**ll** **LL**
size-suffix: one of
**z** **Z**
### Add two new table rows to §6.4.4.1 Integer constants¸ Semantics, ¶6; Modify ¶7 ### {#wording-specification-6.4.4.1p6}
The type of an integer constant is the first of the corresponding list in which its value can be represented.
Suffix Decimal Constant Octal, Binary, or Hexadecimal Constant
… … …
**z** or **Z** the corresponding signed integer type of the expression `sizeof(0)`'s type (6.5.3.4) the corresponding signed integer type of the expression `sizeof(0)`'s type
Both **U** or **U** and **z** or **Z** the same type as the expression `sizeof(0)` the same type as the expression `sizeof(0)`
If an integer constant cannot be represented by any type in its list, it may have an extended integer type, if the extended integer type can represent its value. If all of the types in the list for the constant are signed, the extended integer type shall be signed. If all of the types in the list for the constant are unsigned, the extended integer type shall be unsigned. If the list contains both signed and unsigned types, the extended integer type may be signed or unsigned. If an integer constant cannot be represented by any type in its list and has no extended integer type, then the integer constant has no type. Additionally, if an integer constant with the suffixes **z**, **uz**, **zu**, **Z**, **UZ**, or **ZU** cannot be represented by any type in its list, then the integer constant has no type.
Forward references: preprocessing numbers (6.4.8), numeric conversion functions (7.22.1), The **sizeof** and **_Alignof** operators (6.5.3.4).
{
	"p0330": {
		"authors": [
			"JeanHeyd Meneide",
			"Rein Halbersma"
		],
		"title": "Literal Suffixes for ptrdiff_t and size_t",
		"href": "https://wg21.link/p0330",
		"date": "November 4th, 2019"
	},
	"N2912": {
		"authors": [
			"ISO/IEC JTC1 SC22 WG14 - Programming Languages, C",
			"JeanHeyd Meneide",
			"Freek Wiedijk"
		],
		"title": "N2912: ISO/IEC 9899:202x - Programming Languages, C",
		"href": "https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2912.pdf",
		"date": "June 8th, 2022"
	}
}