diff --git a/stl/inc/cmath b/stl/inc/cmath index a722c06d7f..94813c800b 100644 --- a/stl/inc/cmath +++ b/stl/inc/cmath @@ -616,12 +616,17 @@ _STD _Common_float_type_t<_Ty1, _Ty2> remquo(_Ty1 _Left, _Ty2 _Right, int* _Pquo return _CSTD FUN(static_cast(_Left), _Arg2); \ } +// When the arguments are both floats or both long doubles, overload resolution prefers the +// non-template overloads to the templates generated from this macro. The templates generated from +// this macro are only selected by overload resolution when both arguments have integral type, or +// when the types of the two arguments differ, in which case _Common_float_type_t is either double +// or long double. Note that double and long double have the same underlying representation on our +// supported platforms. #define _GENERIC_MATH2_BASE(NAME, FUN) \ template && _STD is_arithmetic_v<_Ty2>, int> = 0> \ _NODISCARD _STD _Common_float_type_t<_Ty1, _Ty2> NAME(_Ty1 _Left, _Ty2 _Right) noexcept /* strengthened */ { \ - using _Common = _STD _Common_float_type_t<_Ty1, _Ty2>; \ - return FUN(static_cast<_Common>(_Left), static_cast<_Common>(_Right)); \ + return FUN(static_cast(_Left), static_cast(_Right)); \ } #define _GENERIC_MATH2(FUN) _GENERIC_MATH2_BASE(FUN, _CSTD FUN) diff --git a/tests/std/test.lst b/tests/std/test.lst index 9af561348b..0506b1b989 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -217,6 +217,7 @@ tests\GH_002992_unwrappable_iter_sent_pairs tests\GH_003022_substr_allocator tests\GH_003105_piecewise_densities tests\GH_003119_error_category_ctor +tests\GH_003246_cmath_narrowing tests\LWG2597_complex_branch_cut tests\LWG3018_shared_ptr_function tests\LWG3121_constrained_tuple_forwarding_ctor diff --git a/tests/std/tests/GH_003246_cmath_narrowing/env.lst b/tests/std/tests/GH_003246_cmath_narrowing/env.lst new file mode 100644 index 0000000000..19f025bd0e --- /dev/null +++ b/tests/std/tests/GH_003246_cmath_narrowing/env.lst @@ -0,0 +1,4 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +RUNALL_INCLUDE ..\usual_matrix.lst diff --git a/tests/std/tests/GH_003246_cmath_narrowing/test.compile.pass.cpp b/tests/std/tests/GH_003246_cmath_narrowing/test.compile.pass.cpp new file mode 100644 index 0000000000..31b2190311 --- /dev/null +++ b/tests/std/tests/GH_003246_cmath_narrowing/test.compile.pass.cpp @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include + +int main() {} // COMPILE-ONLY + +// Ensure the compiler doesn't warn about narrowing long double to double in GENERIC_MATH2 templates +#define TEST(meow) \ + long double test_##meow(long double x) { return std::meow(x, 1); } + +TEST(atan2) +TEST(hypot) +TEST(pow) +TEST(fmod) +TEST(remainder) +TEST(copysign) +TEST(nextafter) +TEST(fdim) +TEST(fmax) +TEST(fmin)