Edit

Share via


Warning C26447

The function is declared noexcept but calls function function_name that may throw exceptions (f.6).

C++ Core Guidelines:
F.6: If your function may not throw, declare it noexcept.

Remarks

This rule amends another rule, C26440 DECLARE_NOEXCEPT, which tries to find functions that are good candidates to mark as noexcept. In this case, the idea is that once you mark some function as noexcept, it must keep its contract by not invoking other code that may throw exceptions.

  • The Microsoft C++ compiler already handles straightforward violations like throw statements in the function body (see C4297).
  • The rule focuses only on function calls. It flags targets that aren't constexpr and that can potentially throw exceptions. In other words, they aren't marked explicitly as non-throwing by using noexcept, __declspec(nothrow), or throw().
  • The compiler-generated target functions are skipped to reduce noise since exception specifications aren't always provided by the compiler.
  • The checker also skips special kinds of target functions we expect you to implement as noexcept; this rule is enforced by C26439 SPECIAL_NOEXCEPT.

Example

#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;
}

You can fix these warnings by removing noexcept from the function signature.

See also

C26440 DECLARE_NOEXCEPT