mode_has_nan gives MSVC warning C4702 (unreachable code)
Los mantenedores suelen responder en 1 día
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
- Área
- backend, testing-qa
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
- Get Boost.Math
developat commit b8c1f01d or later. - Write a file
t.cppthat callsboost::math::statistics::modeon astd::vector<double>. - 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_statswithout warnings, with C++14 and C++latest. The test passes. - The tests
test_univariate_statisticsandtest_univariate_statistics_backwards_compatiblepass with GCC 16. - In standalone mode with
-fno-exceptions,modegives 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
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de boostorg/math
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
Los mantenedores suelen responder en 1 día
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 12/100
Los mantenedores suelen responder en 1 día
-
Reverse-mode autodiff: order-2 tape slows order-1 gradients, rewound tapes keep allocating blocks, rvar copies silently share a nodePosiblemente ocupada @NAThompson la tomó hace 2 días. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 20/100
Los mantenedores suelen responder en 1 día
-
Feature WishlistAbiertoenhancement
Dificultad 5/5 Más de una semana Aptitud para principiantes 20/100
boostorg/math#303 · 20 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de boostorg/math
Issues similares
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 68/100
eic/TimeframeBuilder#102 ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 65/100
-
error not reported I/O low priority rt_lib
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
Los mantenedores suelen responder en 1 día
-
[XLA] hlo/pass/README.md says passes implement Run; since the NVI change they implement RunImplAbierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 91/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
objectionary/game-of-life#78 ·