public member function
<system_error>

std::error_code::operator bool

explicit operator bool() const noexcept;
轉換為布林值
返回錯誤程式碼是否具有 0 之外的數值value

如果它是零(通常用於表示沒有錯誤),則該函式返回 false,否則它返回 true

引數



返回值

如果錯誤的數值不為零,則為true
否則返回 false

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// error_code::operator bool
#include <iostream>       // std::cout
#include <cerrno>         // errno
#include <system_error>   // std::error_code, std::generic_category
#include <cmath>          // std::pow

struct expnumber {
  double value;
  std::error_code error;
  expnumber (double base, double exponent) {
    value = std::pow(base,exponent);
    if (errno) error.assign (errno,std::generic_category());
  }
};

int main()
{
  expnumber foo (3.0, 2.0), bar (3.0, 10e6);

  std::cout << "foo: ";
  if (!foo.error) std::cout << foo.value << '\n';
  else std::cout << "Error: " << foo.error.message() << '\n';

  std::cout << "bar: ";
  if (!bar.error) std::cout << bar.value << '\n';
  else std::cout << "Error: " << bar.error.message() << '\n';

  return 0;
}

可能的輸出
foo: 9
bar: Error: Result too large


另見