Chỉnh sửa

Chia sẻ qua


Warning C26860

Potentially empty optional 'variable' is unwrapped, may throw exception

Remarks

Unwrapping empty std::optional values via the value method will throw an exception. Such operation can result in a crash when the exception isn't handled. This check will attempt to find cases where a std::optional isn't checked for emptiness before unwrapping it via the value method. You can enable C26829, and C26859 only for a more permissive analysis.

Example

std::optional<int> getOptional();

void f(std::optional<int> maybeEmpty)
{
    if (maybeEmpty)
        maybeEmpty.value() = 42; // No warning
    maybeEmpty.value() = 5; // warning: C26860
    std::optional<int> o = getOptional();
    o.value() = 42; // warning: C26860
}

To solve this problem, make sure the code never unwraps an empty optional.