Rework MSVC warning suppression Previously, MSVC warning C4996 was suppressed unconditionally in the entire code base which made it difficult identifying and fixing specific problems, particularly those in the public interface. Prefer now to disable warnings at the specific location they occur. This approach, however, reveals an inconsistency in how Ceres handles POSIX functions which are declared deprecated by MSVC. Specifically, Bessel functions use the underscore form whereas the read function does not. To simplify the logic, we revert to POSIX compatible functions. C++23 also deprecates std::numeric_limits<T>::has_denorm which MSVC warns about. Here, we disable the deprecation warning locally to avoid the warning leaking into the user code. Fixes #1013 Change-Id: Ida8457cc8dd8770b4384a7c49d16f213b02cdec4
diff --git a/include/ceres/internal/port.h b/include/ceres/internal/port.h index e61ef52..b8cb0ff 100644 --- a/include/ceres/internal/port.h +++ b/include/ceres/internal/port.h
@@ -77,4 +77,15 @@ // #define CERES_PREVENT_MACRO_SUBSTITUTION // Yes, it's empty +// CERES_DISABLE_DEPRECATED_WARNING and CERES_RESTORE_DEPRECATED_WARNING allow +// to temporarily disable deprecation warnings +#if defined(_MSC_VER) +#define CERES_DISABLE_DEPRECATED_WARNING \ + _Pragma("warning(push)") _Pragma("warning(disable : 4996)") +#define CERES_RESTORE_DEPRECATED_WARNING _Pragma("warning(pop)") +#else // defined(_MSC_VER) +#define CERES_DISABLE_DEPRECATED_WARNING +#define CERES_RESTORE_DEPRECATED_WARNING +#endif // defined(_MSC_VER) + #endif // CERES_PUBLIC_INTERNAL_PORT_H_
diff --git a/include/ceres/jet.h b/include/ceres/jet.h index 768f04a..f279ba3 100644 --- a/include/ceres/jet.h +++ b/include/ceres/jet.h
@@ -874,25 +874,19 @@ // function errors in client code (the specific warning is suppressed when // Ceres itself is built). inline double BesselJ0(double x) { -#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS) - return _j0(x); -#else + CERES_DISABLE_DEPRECATED_WARNING return j0(x); -#endif + CERES_RESTORE_DEPRECATED_WARNING } inline double BesselJ1(double x) { -#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS) - return _j1(x); -#else + CERES_DISABLE_DEPRECATED_WARNING return j1(x); -#endif + CERES_RESTORE_DEPRECATED_WARNING } inline double BesselJn(int n, double x) { -#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS) - return _jn(n, x); -#else + CERES_DISABLE_DEPRECATED_WARNING return jn(n, x); -#endif + CERES_RESTORE_DEPRECATED_WARNING } // For the formulae of the derivatives of the Bessel functions see the book: @@ -1310,8 +1304,13 @@ static constexpr bool is_bounded = std::numeric_limits<T>::is_bounded; static constexpr bool is_modulo = std::numeric_limits<T>::is_modulo; + // has_denorm (and has_denorm_loss, not defined for Jet) has been deprecated + // in C++23. However, without an intent to remove the declaration. Disable + // deprecation warnings temporarily just for the corresponding symbols. + CERES_DISABLE_DEPRECATED_WARNING static constexpr std::float_denorm_style has_denorm = std::numeric_limits<T>::has_denorm; + CERES_RESTORE_DEPRECATED_WARNING static constexpr std::float_round_style round_style = std::numeric_limits<T>::round_style;