mode_has_nan gives MSVC warning C4702 (unreachable code)

Cerrado Apto para principiantes
#1,621 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@ibmibmibm ya está trabajando en esto.

Desde el 2/11/2026.

  • #1622 de @ibmibmibm — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
78/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp, csharp

Línea de trabajo

La advertencia proviene de mode_has_nan en include/boost/math/statistics/detail/single_pass.hpp, donde un return true; sigue a BOOST_MATH_THROW_EXCEPTION y es inalcanzable bajo MSVC. Lee esa función y luego compila un archivo pequeño que llame a boost::math::statistics::mode sobre un std::vector<double> con cl /W4 /WX (o GCC con -Wall -Werror si MSVC no está disponible). Está terminado cuando la compilación no tiene advertencias y las pruebas de estadística univariada siguen pasando.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.
Lenguaje dominante
C++
Estrellas
378
Forks
271
Merge medio
12 h 37 min
PR fusionados (30 d)
66

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de boostorg/math

Todos los issues de boostorg/math

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.