-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implement LWG-3834 Missing constexpr for std::intmax_t math functions in <cinttypes>
#6426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Stephan T. Lavavej (StephanTLavavej)
merged 5 commits into
microsoft:main
from
StephanTLavavej:inttypes
Sep 3, 2026
+337
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4992695
Add `__msvc_inttypes.hpp`.
StephanTLavavej fe65735
Implement LWG-3834.
StephanTLavavej 2b0c21b
Test LWG-3834.
StephanTLavavej 7a13a20
Fix the test for /clr:pure.
StephanTLavavej ae0ab6f
Merge branch 'main' into inttypes
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,315 @@ | ||
| // __msvc_inttypes.hpp internal header (core) | ||
|
|
||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| // This file was derived from <inttypes.h> in the Windows 11 SDK (10.0.28000), with some alterations: | ||
| // * Formatted with clang-format. | ||
| // * Changed code to support the MSVC frontend intercepting inclusions of <inttypes.h>. | ||
| // * Changed code for C++23's LWG-3834 "Missing constexpr for std::intmax_t math functions in <cinttypes>". | ||
| // Avoid introducing further unnecessary divergence between <inttypes.h> and this file. | ||
|
|
||
| #ifndef __MSVC_INTTYPES_HPP | ||
| #define __MSVC_INTTYPES_HPP | ||
|
|
||
| // Prevent later inclusions of <inttypes.h> from having any effect. | ||
| #define _INTTYPES | ||
|
|
||
| #include <corecrt.h> | ||
| #include <stdint.h> | ||
|
|
||
| #pragma warning(push) | ||
| #pragma warning(disable : _UCRT_DISABLED_WARNINGS) | ||
| _UCRT_DISABLE_CLANG_WARNINGS | ||
|
|
||
| _CRT_BEGIN_C_HEADER | ||
|
|
||
|
|
||
| //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | ||
| // Types | ||
| // | ||
| //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| typedef struct { | ||
| intmax_t quot; | ||
| intmax_t rem; | ||
| } _Lldiv_t; | ||
|
|
||
| typedef _Lldiv_t imaxdiv_t; | ||
|
|
||
|
|
||
| //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | ||
| // Functions | ||
| // | ||
| //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
|
|
||
| // inttypes functions that became constexpr in C++23 | ||
| #if _HAS_CXX23 && defined(_MSVC_LIBC_MATH) | ||
| #define _CONSTEXPR_CINTTYPES23 constexpr | ||
| #else // ^^^ constexpr in C++23 and later / inline when /Zc:cmath- opts out of constexpr, and in C++20 and earlier vvv | ||
| #define _CONSTEXPR_CINTTYPES23 inline | ||
| #endif // ^^^ inline when /Zc:cmath- opts out of constexpr, and in C++20 and earlier ^^^ | ||
|
|
||
| [[nodiscard]] _Check_return_ _CONSTEXPR_CINTTYPES23 intmax_t __cdecl imaxabs(_In_ intmax_t _Number) noexcept | ||
| /* strengthened */ { | ||
| return _Number >= 0 ? _Number : -_Number; | ||
| } | ||
|
|
||
| [[nodiscard]] _Check_return_ _CONSTEXPR_CINTTYPES23 imaxdiv_t __cdecl imaxdiv( | ||
| _In_ intmax_t _Numerator, _In_ intmax_t _Denominator) noexcept /* strengthened */ { | ||
| return {_Numerator / _Denominator, _Numerator % _Denominator}; | ||
| } | ||
|
|
||
| #undef _CONSTEXPR_CINTTYPES23 | ||
|
|
||
| _ACRTIMP intmax_t __cdecl strtoimax( | ||
| _In_z_ char const* _String, _Out_opt_ _Deref_post_z_ char** _EndPtr, _In_ int _Radix); | ||
|
|
||
| _ACRTIMP intmax_t __cdecl _strtoimax_l( | ||
| _In_z_ char const* _String, _Out_opt_ _Deref_post_z_ char** _EndPtr, _In_ int _Radix, _In_opt_ _locale_t _Locale); | ||
|
|
||
| _ACRTIMP uintmax_t __cdecl strtoumax( | ||
| _In_z_ char const* _String, _Out_opt_ _Deref_post_z_ char** _EndPtr, _In_ int _Radix); | ||
|
|
||
| _ACRTIMP uintmax_t __cdecl _strtoumax_l( | ||
| _In_z_ char const* _String, _Out_opt_ _Deref_post_z_ char** _EndPtr, _In_ int _Radix, _In_opt_ _locale_t _Locale); | ||
|
|
||
| _ACRTIMP intmax_t __cdecl wcstoimax( | ||
| _In_z_ wchar_t const* _String, _Out_opt_ _Deref_post_z_ wchar_t** _EndPtr, _In_ int _Radix); | ||
|
|
||
| _ACRTIMP intmax_t __cdecl _wcstoimax_l(_In_z_ wchar_t const* _String, _Out_opt_ _Deref_post_z_ wchar_t** _EndPtr, | ||
| _In_ int _Radix, _In_opt_ _locale_t _Locale); | ||
|
|
||
| _ACRTIMP uintmax_t __cdecl wcstoumax( | ||
| _In_z_ wchar_t const* _String, _Out_opt_ _Deref_post_z_ wchar_t** _EndPtr, _In_ int _Radix); | ||
|
|
||
| _ACRTIMP uintmax_t __cdecl _wcstoumax_l(_In_z_ wchar_t const* _String, _Out_opt_ _Deref_post_z_ wchar_t** _EndPtr, | ||
| _In_ int _Radix, _In_opt_ _locale_t _Locale); | ||
|
|
||
|
|
||
| //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | ||
| // Output Format Specifier Macros | ||
| // | ||
| //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| #define PRId8 "hhd" | ||
| #define PRId16 "hd" | ||
| #define PRId32 "d" | ||
| #define PRId64 "lld" | ||
| #define PRIdLEAST8 PRId8 | ||
| #define PRIdLEAST16 PRId16 | ||
| #define PRIdLEAST32 PRId32 | ||
| #define PRIdLEAST64 PRId64 | ||
| #define PRIdFAST8 PRId8 | ||
| #define PRIdFAST16 PRId32 | ||
| #define PRIdFAST32 PRId32 | ||
| #define PRIdFAST64 PRId64 | ||
| #define PRIdMAX PRId64 | ||
| #ifdef _WIN64 | ||
| #define PRIdPTR PRId64 | ||
| #else | ||
| #define PRIdPTR PRId32 | ||
| #endif | ||
|
|
||
| #define PRIi8 "hhi" | ||
| #define PRIi16 "hi" | ||
| #define PRIi32 "i" | ||
| #define PRIi64 "lli" | ||
| #define PRIiLEAST8 PRIi8 | ||
| #define PRIiLEAST16 PRIi16 | ||
| #define PRIiLEAST32 PRIi32 | ||
| #define PRIiLEAST64 PRIi64 | ||
| #define PRIiFAST8 PRIi8 | ||
| #define PRIiFAST16 PRIi32 | ||
| #define PRIiFAST32 PRIi32 | ||
| #define PRIiFAST64 PRIi64 | ||
| #define PRIiMAX PRIi64 | ||
| #ifdef _WIN64 | ||
| #define PRIiPTR PRIi64 | ||
| #else | ||
| #define PRIiPTR PRIi32 | ||
| #endif | ||
|
|
||
| #define PRIo8 "hho" | ||
| #define PRIo16 "ho" | ||
| #define PRIo32 "o" | ||
| #define PRIo64 "llo" | ||
| #define PRIoLEAST8 PRIo8 | ||
| #define PRIoLEAST16 PRIo16 | ||
| #define PRIoLEAST32 PRIo32 | ||
| #define PRIoLEAST64 PRIo64 | ||
| #define PRIoFAST8 PRIo8 | ||
| #define PRIoFAST16 PRIo32 | ||
| #define PRIoFAST32 PRIo32 | ||
| #define PRIoFAST64 PRIo64 | ||
| #define PRIoMAX PRIo64 | ||
| #ifdef _WIN64 | ||
| #define PRIoPTR PRIo64 | ||
| #else | ||
| #define PRIoPTR PRIo32 | ||
| #endif | ||
|
|
||
| #define PRIu8 "hhu" | ||
| #define PRIu16 "hu" | ||
| #define PRIu32 "u" | ||
| #define PRIu64 "llu" | ||
| #define PRIuLEAST8 PRIu8 | ||
| #define PRIuLEAST16 PRIu16 | ||
| #define PRIuLEAST32 PRIu32 | ||
| #define PRIuLEAST64 PRIu64 | ||
| #define PRIuFAST8 PRIu8 | ||
| #define PRIuFAST16 PRIu32 | ||
| #define PRIuFAST32 PRIu32 | ||
| #define PRIuFAST64 PRIu64 | ||
| #define PRIuMAX PRIu64 | ||
| #ifdef _WIN64 | ||
| #define PRIuPTR PRIu64 | ||
| #else | ||
| #define PRIuPTR PRIu32 | ||
| #endif | ||
|
|
||
| #define PRIx8 "hhx" | ||
| #define PRIx16 "hx" | ||
| #define PRIx32 "x" | ||
| #define PRIx64 "llx" | ||
| #define PRIxLEAST8 PRIx8 | ||
| #define PRIxLEAST16 PRIx16 | ||
| #define PRIxLEAST32 PRIx32 | ||
| #define PRIxLEAST64 PRIx64 | ||
| #define PRIxFAST8 PRIx8 | ||
| #define PRIxFAST16 PRIx32 | ||
| #define PRIxFAST32 PRIx32 | ||
| #define PRIxFAST64 PRIx64 | ||
| #define PRIxMAX PRIx64 | ||
| #ifdef _WIN64 | ||
| #define PRIxPTR PRIx64 | ||
| #else | ||
| #define PRIxPTR PRIx32 | ||
| #endif | ||
|
|
||
| #define PRIX8 "hhX" | ||
| #define PRIX16 "hX" | ||
| #define PRIX32 "X" | ||
| #define PRIX64 "llX" | ||
| #define PRIXLEAST8 PRIX8 | ||
| #define PRIXLEAST16 PRIX16 | ||
| #define PRIXLEAST32 PRIX32 | ||
| #define PRIXLEAST64 PRIX64 | ||
| #define PRIXFAST8 PRIX8 | ||
| #define PRIXFAST16 PRIX32 | ||
| #define PRIXFAST32 PRIX32 | ||
| #define PRIXFAST64 PRIX64 | ||
| #define PRIXMAX PRIX64 | ||
| #ifdef _WIN64 | ||
| #define PRIXPTR PRIX64 | ||
| #else | ||
| #define PRIXPTR PRIX32 | ||
| #endif | ||
|
|
||
|
|
||
| //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | ||
| // Input Format Specifier Macros | ||
| // | ||
| //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| #define SCNd8 "hhd" | ||
| #define SCNd16 "hd" | ||
| #define SCNd32 "d" | ||
| #define SCNd64 "lld" | ||
| #define SCNdLEAST8 SCNd8 | ||
| #define SCNdLEAST16 SCNd16 | ||
| #define SCNdLEAST32 SCNd32 | ||
| #define SCNdLEAST64 SCNd64 | ||
| #define SCNdFAST8 SCNd8 | ||
| #define SCNdFAST16 SCNd32 | ||
| #define SCNdFAST32 SCNd32 | ||
| #define SCNdFAST64 SCNd64 | ||
| #define SCNdMAX SCNd64 | ||
| #ifdef _WIN64 | ||
| #define SCNdPTR SCNd64 | ||
| #else | ||
| #define SCNdPTR SCNd32 | ||
| #endif | ||
|
|
||
| #define SCNi8 "hhi" | ||
| #define SCNi16 "hi" | ||
| #define SCNi32 "i" | ||
| #define SCNi64 "lli" | ||
| #define SCNiLEAST8 SCNi8 | ||
| #define SCNiLEAST16 SCNi16 | ||
| #define SCNiLEAST32 SCNi32 | ||
| #define SCNiLEAST64 SCNi64 | ||
| #define SCNiFAST8 SCNi8 | ||
| #define SCNiFAST16 SCNi32 | ||
| #define SCNiFAST32 SCNi32 | ||
| #define SCNiFAST64 SCNi64 | ||
| #define SCNiMAX SCNi64 | ||
| #ifdef _WIN64 | ||
| #define SCNiPTR SCNi64 | ||
| #else | ||
| #define SCNiPTR SCNi32 | ||
| #endif | ||
|
|
||
| #define SCNo8 "hho" | ||
| #define SCNo16 "ho" | ||
| #define SCNo32 "o" | ||
| #define SCNo64 "llo" | ||
| #define SCNoLEAST8 SCNo8 | ||
| #define SCNoLEAST16 SCNo16 | ||
| #define SCNoLEAST32 SCNo32 | ||
| #define SCNoLEAST64 SCNo64 | ||
| #define SCNoFAST8 SCNo8 | ||
| #define SCNoFAST16 SCNo32 | ||
| #define SCNoFAST32 SCNo32 | ||
| #define SCNoFAST64 SCNo64 | ||
| #define SCNoMAX SCNo64 | ||
| #ifdef _WIN64 | ||
| #define SCNoPTR SCNo64 | ||
| #else | ||
| #define SCNoPTR SCNo32 | ||
| #endif | ||
|
|
||
| #define SCNu8 "hhu" | ||
| #define SCNu16 "hu" | ||
| #define SCNu32 "u" | ||
| #define SCNu64 "llu" | ||
| #define SCNuLEAST8 SCNu8 | ||
| #define SCNuLEAST16 SCNu16 | ||
| #define SCNuLEAST32 SCNu32 | ||
| #define SCNuLEAST64 SCNu64 | ||
| #define SCNuFAST8 SCNu8 | ||
| #define SCNuFAST16 SCNu32 | ||
| #define SCNuFAST32 SCNu32 | ||
| #define SCNuFAST64 SCNu64 | ||
| #define SCNuMAX SCNu64 | ||
| #ifdef _WIN64 | ||
| #define SCNuPTR SCNu64 | ||
| #else | ||
| #define SCNuPTR SCNu32 | ||
| #endif | ||
|
|
||
| #define SCNx8 "hhx" | ||
| #define SCNx16 "hx" | ||
| #define SCNx32 "x" | ||
| #define SCNx64 "llx" | ||
| #define SCNxLEAST8 SCNx8 | ||
| #define SCNxLEAST16 SCNx16 | ||
| #define SCNxLEAST32 SCNx32 | ||
| #define SCNxLEAST64 SCNx64 | ||
| #define SCNxFAST8 SCNx8 | ||
| #define SCNxFAST16 SCNx32 | ||
| #define SCNxFAST32 SCNx32 | ||
| #define SCNxFAST64 SCNx64 | ||
| #define SCNxMAX SCNx64 | ||
| #ifdef _WIN64 | ||
| #define SCNxPTR SCNx64 | ||
| #else | ||
| #define SCNxPTR SCNx32 | ||
| #endif | ||
|
|
||
|
|
||
| _CRT_END_C_HEADER | ||
| _UCRT_RESTORE_CLANG_WARNINGS | ||
| #pragma warning(pop) // _UCRT_DISABLED_WARNINGS | ||
| #endif // __MSVC_INTTYPES_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.