Repository navigation
<math.h>: Provide the abs() overloads under /Zc:cmath - #6535
Draft
Tiago (tiagomacarios) wants to merge 1 commit into
Draft
Tiago (tiagomacarios) wants to merge 1 commit into
Tiago (tiagomacarios) wants to merge 1 commit into
Conversation
Fixes microsoft#6533. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #6533.
AI disclosure: the header and test changes and this description were written with GitHub Copilot, and checked as described under Testing.
Opening as a draft: this touches
<__msvc_stdlib.hpp>, which is meant to stay close to the SDK's<stdlib.h>, so you may prefer a different shape (alternatives below), or to make the change yourselves.Under
/Zc:cmath,<math.h>declares noabsoverloads: the integer ones are in<__msvc_stdlib.hpp>and the floating-point ones in<cstdlib>. [support.c.headers.other]/1 requires<math.h>to provide theabsoverloads that<cmath>declares, andabs(int),labsandllabsworked with the UCRT's<math.h>.This change:
abs(int),abs(long),abs(long long),labsandllabsfrom<__msvc_stdlib.hpp>into a new<__msvc_int_abs.hpp>, unchanged and in the same_Msvc_stdlibinline namespace, included at file scope by both<__msvc_stdlib.hpp>and<__msvc_math.hpp>;absoverloads in<__msvc_math.hpp>as the same global functions<cstdlib>defines, and has<cstdlib>skip its copies when<__msvc_math.hpp>is in use.Alternatives:
<__msvc_math.hpp>including<__msvc_stdlib.hpp>(one line, but brings all of<stdlib.h>into<math.h>); duplicating the integer definitions in both headers under a shared guard macro; or one<__msvc_abs.hpp>with all six overloads, which would also give<stdlib.h>the floating-point ones (it lacks them today in every mode).The new header includes only UCRT headers, like
<__msvc_stdlib.hpp>, so<stdlib.h>doesn't start including<yvals_core.h>: code that macroizes a keyword before<stdlib.h>keeps compiling.Testing: new compile-only
GH_006533_math_h_abschecks the sixabsoverloads,labsandllabswith<math.h>alone, with<stdlib.h>before or after, and with<cmath>after, at C++14 through latest, usingNO_TEST_ENVIRONMENT_PREPARERsoforce_include.hpp's<stdlib.h>doesn't hide the problem. Locally with MSVC Build Tools 14.52.37025 (not the Preview toolset): it fails 15 of 24 configurations onmainand passes all 24 with the change.P0533R9_constexpr_for_cmath_and_cstdlib,VSO_0000000_c_math_functions,GH_000519_cmath_overloads,GH_004609_heterogeneous_cmp_overloads,GH_001411_core_headers(these four also with/Zc:cmathadded) andVSO_0157762_feature_test_macrospass. With/Zc:cmath, all header units build in topological order as inP1502R1,import std.compat;sees all six overloads, and#include <math.h>followed byimport std;goes from C2668 to compiling. Not run: clang-cl, ASan,/clr,/BE,/analyze, the libc++ suite. clang-format andvalidateare clean.