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) :
std::strcmp(what(), what_arg.c_str()) == 0 - (2) :
std::strcmp(what(), what_arg) == 0
例外
- (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
関連項目
参照
- P3378R2
constexprexception types- C++26で
constexpr対応した
- C++26で