共用方式為


警告 C26447

函式已宣告, noexcept 但會呼叫可能會擲回例外狀況的函 function_name (f.6)。

C++核心指導方針:
F.6:如果您的函式可能未擲回,請宣告它 noexcept

備註

此規則會修改另一個規則 C26440 DECLARE_NOEXCEPT,它會嘗試尋找要標示為 noexcept的好候選函式。 在此情況下,這個想法是,一旦您將某些函式標示為 noexcept,它必須保留其合約,而不叫用其他可能會擲回例外狀況的程序代碼。

  • Microsoft C++編譯程式已經處理直接違規,例如 throw 函式主體中的語句(請參閱 C4297)。
  • 規則只著重於函數調用。 它會標幟不是 constexpr 且可能會擲回例外狀況的目標。 換句話說,它們不會使用 noexcept__declspec(nothrow)throw() 明確標示為非擲回。
  • 編譯程式產生的目標函式會略過以減少雜訊,因為編譯程式不一定會提供例外狀況規格。
  • 檢查程式也會略過我們預期您實作為 noexcept的特殊目標函式類型;此規則是由 C26439 SPECIAL_NOEXCEPT強制執行。

範例

#include <vector>
#include <string>
#include <istream>

std::vector<std::string> collect(std::istream& is) noexcept
{
    std::vector<std::string> res;
    for (std::string s; is >> s;) // C26447, `operator bool()` can  throw, std::string's allocator can throw
        res.push_back(s);         // C26447, `push_back` can throw
    return res;
}

您可以從函式簽章中移除 noexcept 來修正這些警告。

另請參閱

C26440 DECLARE_NOEXCEPT