Namespaces
Variants

std:: invalid_argument

From cppreference.net
定义于头文件 <stdexcept>
class invalid_argument ;

定义一种作为异常抛出的对象类型。它报告由于参数值未被接受而引发的错误。

此异常由 std::bitset::bitset ,以及 std::stoi std::stof 系列函数抛出。

std::invalid_argument 的所有成员函数都是 constexpr :可以在常量表达式求值过程中创建和使用 std::invalid_argument 对象。

然而, std::invalid_argument 对象通常不能是 constexpr ,因为任何动态分配的存储必须在同一常量表达式求值过程中释放。

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

继承关系图

目录

成员函数

(constructor)
使用给定消息构造新的 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:: 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++标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 254 C++98 缺少接受 const char * 的构造函数 已添加
LWG 471 C++98 std::invalid_argument 副本的说明字符串
由实现定义
与原始 std::invalid_argument 对象
的说明字符串相同