mode_has_nan gives MSVC warning C4702 (unreachable code)

Aperta Adatta ai principianti
#1,621 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@ibmibmibm ci sta già lavorando.

Dal 2/11/2026.

  • #1622 di @ibmibmibm — aperta

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
Stack tecnologico
cpp, csharp

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
  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.
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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di boostorg/math

Tutte le issue di boostorg/math

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.