Skip to content

Latest commit

 

History

History
60 lines (50 loc) · 1.35 KB

nothrow_t.md

File metadata and controls

60 lines (50 loc) · 1.35 KB

nothrow_t

  • new[meta header]
  • std[meta namespace]
  • class[meta id-type]
namespace std {
  struct nothrow_t { explicit nothrow_t() = default; };
  extern const nothrow_t nothrow;
}

概要

nothrow_tは、new失敗時に例外を送出させないための型である。

nothrowは、std::nothrow_t型の定数であり、newキーワードに指定するタグとして使用する。

#include <iostream>
#include <new>

struct ThrowObj
{
  ThrowObj()
  {
    throw std::logic_error("logic_error: ThrowObj::ThrowObj()");
  }
};

int main()
{
  // 長さ3の動的配列を作成する
  // 領域確保に失敗した場合、nullptrが返される
  int* p = new(std::nothrow) int[3];

  // ただし、オブジェクトのコンストラクタが例外を投げる場合は
  // 例外がスローされることに注意
  try
  {
    ThrowObj* obj = new(std::nothrow) ThrowObj();
    delete obj;
  }
  catch (std::logic_error& e)
  {
    // この場合でもnew(std::nothrow)で確保されたメモリは解放されている。
    std::cout << e.what() << std::endl;
  }
}
  • std::nothrow[color ff0000]
  • std::logic_error[link /reference/stdexcept.md]

出力

logic_error: ThrowObj::ThrowObj()

参照