最終更新日時:
が更新

履歴 編集

function
<stdexcept>

std::range_error::コンストラクタ

explicit range_error(const string& what_arg);           // (1) C++98
constexpr explicit range_error(const string& what_arg); // (1) C++26

explicit range_error(const char* what_arg);             // (2) C++11
constexpr explicit range_error(const char* what_arg);   // (2) C++26

range_error(const range_error&) noexcept;                     // (3) C++98
constexpr range_error(const range_error&) noexcept;           // (3) C++26

概要

range_errorオブジェクトを構築する。

  • (1) : エラー理由の文字列をstd::stringで指定して構築する
  • (2) : エラー理由の文字列をヌル終端の文字列で指定して構築する
  • (3) : コピーコンストラクタ

事後条件

例外

  • (1), (2) : 文字列の複製で記憶域の確保が必要になる場合、std::bad_allocを送出する可能性がある
  • (3) : 投げない

備考

  • (2) : C++11で追加された。文字列リテラルからstd::stringの一時オブジェクトを生成せずに構築できる

#include <stdexcept>
#include <iostream>
#include <string>

int main()
{
  std::range_error e1{"error message"};              // (2) ヌル終端文字列から構築
  std::range_error e2{std::string{"error message"}}; // (1) std::stringから構築

  std::cout << e1.what() << std::endl;
  std::cout << e2.what() << std::endl;
}

出力

error message
error message

関連項目

参照