Skip to content

Latest commit

 

History

History
44 lines (33 loc) · 2.12 KB

c26447.md

File metadata and controls

44 lines (33 loc) · 2.12 KB
title description ms.date f1_keywords helpviewer_keywords
Warning C26447
Microsoft C++ Code Analysis warning C26447 for the C++ Core Guidelines case F.6.
08/25/2020
C26447
DONT_THROW_IN_NOEXCEPT
C26447

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