公共成員函式
<system_error>

std::error_code::assign

void assign (int val, const error_category& cat) noexcept;
分配錯誤碼
error_code物件賦值為與error_category cat關聯的值val

可以使用賦值運算子 (=),透過使用列舉值來為error_code物件分配一個新值。

引數

val
一個用於標識錯誤碼的數值。
cat
error_category 物件的引用。

返回值



示例

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
// error_code::assign
#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);
  std::cout << foo.value << "\t" << foo.error.message() << '\n';

  expnumber bar (3.0, 10e6);
  std::cout << bar.value << "\t" << bar.error.message() << '\n';

  return 0;
}

可能的輸出
9       No error
inf     Result too large


另見