名前空間
変種
操作

std::invalid_argument

From cppreference.com
< cpp‎ | error
 
 
 
ヘッダー <stdexcept> で定義
class invalid_argument;

引数の値が受け入れられなかったために発生するエラーを報告する、例外としてスローされるオブジェクトの型を定義します。

この例外は、 std::bitset::bitset、および std::stoi および std::stof ファミリの関数によってスローされます。

cpp/error/exceptioncpp/error/logic errorstd-invalid argument-inheritance.svg

継承図

目次

[編集] メンバ関数

(コンストラクタ)
指定されたメッセージで新しいinvalid_argumentオブジェクトを構築します。
(public member function)
operator=
invalid_argumentオブジェクトを置き換えます。
(public member function)

std::invalid_argument::invalid_argument

invalid_argument( const std::string& what_arg );
(1)
invalid_argument( const char* what_arg );
(2)
invalid_argument( const invalid_argument& other );
(3) (C++11 以降 noexcept)
1) 説明文字列として what_arg を使用して、例外オブジェクトを構築します。構築後、std::strcmp(what(), what_arg.c_str()) == 0 となります。
2) 説明文字列として what_arg を使用して、例外オブジェクトを構築します。構築後、std::strcmp(what(), what_arg) == 0 となります。
3) コピーコンストラクタ。 *thisother が両方とも動的型 std::invalid_argument を持つ場合、 std::strcmp(what(), other.what()) == 0 になります。コピーコンストラクタから例外がスローされることはありません。

パラメータ

what_arg - 説明文字列
その他 - コピーする別の例外オブジェクト

例外

1,2) std::bad_alloc をスローする可能性があります。

注釈

std::invalid_argument のコピーは例外をスローすることが許可されていないため、このメッセージは通常、内部的に別途割り当てられた参照カウント文字列として格納されます。これも、std::string&& を受け取るコンストラクタがない理由です。いずれにしても内容をコピーする必要があるからです。

LWG issue 254 の解決前は、非コピーコンストラクタは std::string のみを受け入れることができました。これにより、std::string オブジェクトを構築するために動的割り当てが必須となりました。

LWG issue 471 の解決後、派生標準例外クラスは公開アクセス可能なコピーコンストラクタを持つ必要があります。what() によって取得される説明文字列が元のオブジェクトとコピーされたオブジェクトで同じである限り、暗黙的に定義することができます。

std::invalid_argument::operator=

invalid_argument& operator=( const invalid_argument& other );
(C++11 以降 noexcept)

other の内容で内容を代入します。 *thisother が両方とも動的型 std::invalid_argument を持つ場合、代入後には std::strcmp(what(), other.what()) == 0 となります。コピー代入演算子から例外がスローされることはありません。

パラメータ

その他 - 割り当てる別の例外オブジェクト

戻り値

*this

注釈

LWG issue 471 の解決後、派生標準例外クラスは公開アクセス可能なコピー代入演算子を持つ必要があります。what() によって取得される説明文字列が元のオブジェクトとコピーされたオブジェクトで同じである限り、暗黙的に定義することができます。

std::logic_error から継承

std::exception から継承

メンバ関数

例外オブジェクトを破棄する
(std::exception の仮想 public メンバー関数) [編集]
[virtual]
説明文字列を返す
(std::exception の仮想 public メンバー関数) [編集]

[編集] 注記

この例外型の目的は、エラー状態 std::errc::invalid_argumentstd::system_errorstd::thread のメンバ関数からスローされる)および関連する errno 定数 EINVAL と似ています。

[編集]

#include <bitset>
#include <iostream>
#include <stdexcept>
#include <string>
 
int main()
{
    try
    {
        std::bitset<4>{"012"}; // Throws: only '0' or '1' expected
    }
    catch (std::invalid_argument const& ex)
    {
        std::cout << "#1: " << ex.what() << '\n';
    }
 
    try
    {
        [[maybe_unused]] int f = std::stoi("ABBA"); // Throws: no conversion
    }
    catch (std::invalid_argument const& ex)
    {
        std::cout << "#2: " << ex.what() << '\n';
    }
 
    try
    {
        [[maybe_unused]] float f = std::stof("(3.14)"); // Throws: no conversion
    }
    catch (std::invalid_argument const& ex)
    {
        std::cout << "#3: " << ex.what() << '\n';
    }
}

実行結果の例

#1: bitset string ctor has invalid argument
#2: stoi: no conversion
#3: stof: no conversion

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 254 C++98 const char* を受け入れるコンストラクタが欠落していた 追加された
LWG 471 C++98 std::invalid_argument の説明文字列
コピーは実装定義であった
それらは元の std::domain_error オブジェクトと
元の std::invalid_argument オブジェクト
English 日本語 中文(简体) 中文(繁體)