Namespaces
Variants

std:: invalid_argument

From cppreference.net
ヘッダーで定義 <stdexcept>
class invalid_argument ;

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

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

std::invalid_argumentのすべてのメンバー関数は std::invalid_argument constexpr です:定数式の評価中に std::invalid_argument オブジェクトを作成して使用することが可能です。

しかしながら、 std::invalid_argument オブジェクト自体は一般的に constexpr にはなりません。なぜなら、動的に確保されたストレージはすべて同じ定数式の評価中に解放されなければならないためです。

(C++26以降)
cpp/error/exception cpp/error/logic error std-invalid argument-inheritance.svg

継承図

目次

メンバー関数

(コンストラクタ)
指定されたメッセージで新しい invalid_argument オブジェクトを構築する
(公開メンバ関数)
operator=
invalid_argument オブジェクトを置き換える
(公開メンバ関数)

std::invalid_argument:: invalid_argument

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

パラメータ

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

例外

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 ) ;
(noexcept since C++11)
(constexpr since C++26)

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

パラメータ

other - 代入する別の例外オブジェクト

戻り値

* this

注記

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

std:: logic_error から継承

std::exception から継承 std:: exception

メンバ関数

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

注記

この例外型の目的は、エラー条件 std::errc::invalid_argument std::system_error において std::thread のメンバ関数からスローされる)および関連するerrno定数 EINVAL と同様です。

機能テスト マクロ 標準 機能
__cpp_lib_constexpr_exceptions 202502L (C++26) constexpr 例外型

#include <bitset>
#include <iostream>
#include <stdexcept>
#include <string>
int main()
{
    try
    {
        std::bitset<4>{"012"}; // スロー: '0' または '1' のみが期待されます
    }
    catch (std::invalid_argument const& ex)
    {
        std::cout << "#1: " << ex.what() << '\n';
    }
    try
    {
        [[maybe_unused]] int f = std::stoi("ABBA"); // スロー: 変換できません
    }
    catch (std::invalid_argument const& ex)
    {
        std::cout << "#2: " << ex.what() << '\n';
    }
    try
    {
        [[maybe_unused]] float f = std::stof("(3.14)"); // スロー: 変換できません
    }
    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::invalid_argument オブジェクトの
説明文字列と同じである