Skip to content

Fix the MSVC warning and the no-exceptions link error in mode_has_nan - #1622

Merged
NAThompson merged 1 commit into
boostorg:developfrom
ibmibmibm:mode-nan-unreachable
Nov 3, 2026
Merged

NAThompson merged 1 commit into
boostorg:developfrom
ibmibmibm:mode-nan-unreachable

Conversation

@ibmibmibm

@ibmibmibm ibmibmibm commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1621

  • MSVC gave C4702 (unreachable code) for the return true; after boost::throw_exception. This stopped builds that treat warnings as errors, for example the Boost.Decimal CI. Keep the result of the NaN search in mode_has_nan in a variable, and return it after the throw, so that no statement follows the throw.
  • Without exceptions, Boost requires the program to define boost::throw_exception, so programs that call mode did not link. The Boost.Decimal -fno-exceptions CI job failed for this reason (test_boost_math_univariate_stats does not link with -fno-exceptions decimal#1547), and a Boost.Decimal maintainer asked for the fix in Boost.Math (Define boost::throw_exception in the univariate statistics test decimal#1548). Throw only if BOOST_MATH_NO_EXCEPTIONS is not set, as the policies do, which select errno_on_error by default.
  • Without exceptions, the function returns true for data that contain a NaN, and mode gives no result. Standalone mode already did this. Document this.
  • Add git_issue_1621.cpp, which runs with and without exceptions, and add it to no_eh_tests.
Tests
  • MSVC 19.51 (x64, /W4 /WX): the Boost.Decimal test test_boost_math_univariate_stats fails with C4702 on develop. With this change it compiles and passes, with C++14 and C++latest.
  • GCC 16.2 and Clang 23.1, C++14 and C++20, with and without exceptions: the same Boost.Decimal test links and passes, with no change in Boost.Decimal. On develop, the -fno-exceptions build fails with an undefined reference to boost::throw_exception(std::exception const&).
  • GCC 16.2: test_univariate_statistics (C++17) and test_univariate_statistics_backwards_compatible (C++14) pass.
  • git_issue_1621, GCC 16.2 and Clang 23.1, C++14: passes with exceptions, and with -fno-exceptions -fno-rtti and noeh_support.cpp as in no_eh_tests. On develop, the no-exceptions run aborts. MSVC 19.51 with /EHsc passes.
  • GCC 16.2 and Clang 23.1 with -fno-exceptions, in the Boost and the standalone configuration: a program that calls mode links without a definition of boost::throw_exception. mode gives no result for data that contain a NaN, and the correct mode for other data.

- MSVC gave C4702 (unreachable code) for the return after
  boost::throw_exception, which stopped builds that treat warnings as
  errors. Keep the result of the NaN search in a variable, and return
  it after the throw, so that no statement follows the throw.
- Without exceptions, Boost requires the program to define
  boost::throw_exception, so programs that call mode did not link.
  Throw only if BOOST_MATH_NO_EXCEPTIONS is not set, as the policies
  do, which select errno_on_error by default.
- Without exceptions, the function returns true for data that contain
  a NaN, and mode gives no result. Document this.
- Add a test that runs with and without exceptions, and add it to
  no_eh_tests.

Fixes boostorg#1621
@ibmibmibm
ibmibmibm force-pushed the mode-nan-unreachable branch from 95cda7c to 9c264e0 Compare November 2, 2026 18:12
@ibmibmibm ibmibmibm changed the title Remove the unreachable return after the throw in mode_has_nan Fix the MSVC warning and the no-exceptions link error in mode_has_nan Nov 2, 2026

@mborland mborland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Is there anything else you were planning to add before marking it as ready? Seems sufficient for the issue.

@ibmibmibm
ibmibmibm marked this pull request as ready for review November 2, 2026 22:37
@NAThompson
NAThompson merged commit 3f08b0f into boostorg:develop Nov 3, 2026
49 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mode_has_nan gives MSVC warning C4702 (unreachable code)

3 participants