mode_has_nan gives MSVC warning C4702 (unreachable code)
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- backend, testing-qa
Direzione di ricerca
L'avviso proviene da mode_has_nan in include/boost/math/statistics/detail/single_pass.hpp, dove un return true; segue BOOST_MATH_THROW_EXCEPTION ed è irraggiungibile con MSVC. Leggi quella funzione, quindi compila un piccolo file che chiama boost::math::statistics::mode su un std::vector<double> con cl /W4 /WX (o GCC con -Wall -Werror se MSVC non è disponibile). È completato quando la build non produce avvisi e i test di statistica univariata continuano a passare.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- C++
- Stelle
- 377
- Fork
- 269
- Merge medio
- 12h 52m
- PR unite (30g)
- 60
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di boostorg/math
-
Add multivariate root finding (Newton and Cuyt's Halley)Forse già presa @NAThompson l’ha presa oggi. Aperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 12/100
I maintainer di solito rispondono entro 1 giorno
-
Add a trust-region Newton optimizerForse già presa @NAThompson l’ha presa oggi. Aperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 15/100
I maintainer di solito rispondono entro 1 giorno
-
hypergeometric_0F1 loses about 20 ulps for b = 1.5 and −10 < z < 0Forse già presa @NAThompson l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 22/100
I maintainer di solito rispondono entro 1 giorno
-
Feature WishlistApertaenhancement
Difficoltà 5/5 Più di una settimana Idoneità per principianti 20/100
boostorg/math#303 · 18 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di boostorg/math
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
eclipse-score/lifecycle#929 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
colbymchenry/codegraph#3223 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
firefox-devtools/profiler#6436 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
The `ufw` decoder requires the kernel's monotonic timestamp, so UFW lines read from journald decode without any fieldForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno