Skip to content

Latest commit

 

History

History
75 lines (57 loc) · 1.51 KB

op_bool.md

File metadata and controls

75 lines (57 loc) · 1.51 KB

explicit operator bool

  • system_error[meta header]
  • std[meta namespace]
  • error_code[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
explicit operator bool() const noexcept;

概要

error_codeオブジェクトがエラー状態であるかを判定する。

error_codeクラスのデフォルトエラー値である0が正常と見なされ、それ以外の場合はエラーと見なされる。

trueの場合はエラーであることを意味し、falseの場合は正常を意味する。

戻り値

value() != 0

例外

投げない

#include <iostream>
#include <system_error>
#include <string>

void print(const std::error_code& ec)
{
  if (ec) {
    std::cout << "error! : " << ec.message() << std::endl;
  }
  else {
    std::cout << "success" << std::endl;
  }
}

int main()
{
  std::error_code err1;
  print(err1);

  std::error_code err2(static_cast<int>(std::errc::invalid_argument),
                       std::generic_category());
  print(err2);
}
  • if (ec)[color ff0000]
  • std::errc::invalid_argument[link /reference/system_error/errc.md]
  • std::generic_category()[link /reference/system_error/generic_category.md]

出力

success
error! : Invalid argument

バージョン

言語

  • C++11

処理系

参照