mode_has_nan gives MSVC warning C4702 (unreachable code)

Đã đóng Phù hợp với người mới
#1,621 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@ibmibmibm đang làm issue này rồi.

Từ ngày 2/11/2026.

  • #1622 của @ibmibmibm — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp, csharp
Lĩnh vực
backend, testing-qa

Hướng nghiên cứu

Cảnh báo đến từ mode_has_nan trong include/boost/math/statistics/detail/single_pass.hpp, nơi return true; nằm sau BOOST_MATH_THROW_EXCEPTION và không thể đạt tới khi dùng MSVC. Hãy đọc hàm đó, sau đó biên dịch một tệp nhỏ gọi boost::math::statistics::mode trên std::vector<double> bằng cl /W4 /WX (hoặc GCC với -Wall -Werror nếu không có MSVC). Hoàn thành khi bản build không có cảnh báo và các bài kiểm thử thống kê đơn biến vẫn đều đạt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

mode_has_nan on develop (471bef4f) gives MSVC warning C4702 (unreachable code). Pull request #1607 (commit b8c1f01d) added this function. A build that sets warnings as errors stops at this warning. Because of this, the Boost.Decimal test test_boost_math_univariate_stats does not compile with MSVC 14.2, 14.3 and 14.5.

Cause

In mode_has_nan (include/boost/math/statistics/detail/single_pass.hpp:374), a return true; statement follows BOOST_MATH_THROW_EXCEPTION:

if (std::any_of(first, last, [](const Z& z) { return !(z == z); }))
{
    BOOST_MATH_THROW_EXCEPTION(std::domain_error("The mode is undefined for data containing a NaN."))
    return true;
}
return false;

In the usual (not standalone) configuration, BOOST_MATH_THROW_EXCEPTION calls boost::throw_exception. This function does not return. Thus MSVC shows warning C4702 for the return true; statement.

Reproduction
  1. Get Boost.Math develop at commit b8c1f01d or later.
  2. Write a file t.cpp that calls boost::math::statistics::mode on a std::vector<double>.
  3. Compile the file with MSVC: cl /W4 /WX /EHsc /std:c++17 t.cpp.

Output with MSVC 19.51.36260 for x64:

single_pass.hpp(375) : error C2220: the following warning is treated as an error
single_pass.hpp(375) : warning C4702: unreachable code

The Boost.Decimal CI shows the same error with MSVC 14.2, 14.3 and 14.5, for 32-bit and 64-bit.

Expected result

The file compiles without warnings.

Proposed fix

Keep the result of the search in a variable. Return the variable after the throw. Then no statement follows the throw:

const bool has_nan = std::any_of(first, last, [](const Z& z) { return !(z == z); });
if (has_nan)
{
    BOOST_MATH_THROW_EXCEPTION(std::domain_error("The mode is undefined for data containing a NaN."))
}
return has_nan;

In standalone mode with exceptions off, BOOST_MATH_THROW_EXCEPTION expands to nothing. With the fix, the function continues to return true for data that contain a NaN.

We did these tests with the fix:

  • MSVC compiles the Boost.Decimal test test_boost_math_univariate_stats without warnings, with C++14 and C++latest. The test passes.
  • The tests test_univariate_statistics and test_univariate_statistics_backwards_compatible pass with GCC 16.
  • In standalone mode with -fno-exceptions, mode gives the same results before and after the fix. It gives no mode for data that contain a NaN.
Ngôn ngữ chính
C++
Star
378
Fork
271
Merge trung bình
12 giờ 31 phút
Pull request đã merge (30 ngày)
70

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của boostorg/math

Tất cả issue của boostorg/math

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.