Namespaces
Variants

std:: invalid_argument

From cppreference.net
헤더 파일에 정의됨 <stdexcept>
class invalid_argument ;

인수 값이 허용되지 않아 발생하는 오류를 보고하는 예외로 던져질 객체 유형을 정의합니다.

이 예외는 std::bitset::bitset std::stoi std::stof 함수 계열에 의해 throw됩니다.

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 객체를 생성합니다
(public 멤버 함수)
operator=
invalid_argument 객체를 대체합니다
(public 멤버 함수)

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 이슈 254 의 해결 이전에는, 비복사 생성자는 std::string 만 받아들일 수 있었습니다. 이는 std::string 객체를 생성하기 위해 동적 할당을 필수적으로 만들었습니다.

LWG 이슈 471 의 해결 이후, 파생된 표준 예외 클래스는 공개적으로 접근 가능한 복사 생성자를 가져야 합니다. 원본 객체와 복사된 객체에 대해 what() 으로 얻은 설명 문자열이 동일한 한 암시적으로 정의될 수 있습니다.

std::invalid_argument:: operator=

invalid_argument & operator = ( const invalid_argument & other ) ;
(C++11부터 noexcept)
(C++26부터 constexpr)

내용을 other 의 내용으로 할당합니다. * this other 모두 동적 타입이 std::invalid_argument 인 경우, 할당 후 std:: strcmp ( what ( ) , other. what ( ) ) == 0 입니다. 복사 할당 연산자에서는 예외가 발생하지 않습니다.

매개변수

other - 할당할 다른 예외 객체

반환값

* this

참고

LWG 이슈 471 의 해결 이후, 파생된 표준 예외 클래스는 공개적으로 접근 가능한 복사 할당 연산자를 가져야 합니다. what() 으로 얻은 설명 문자열이 원본 객체와 복사된 객체에 대해 동일한 경우 암시적으로 정의될 수 있습니다.

std:: logic_error 에서 상속됨

std:: exception 로부터 상속됨

멤버 함수

[virtual]
예외 객체를 파괴함
( std::exception 의 virtual public 멤버 함수)
[virtual]
설명 문자열을 반환함
( std::exception 의 virtual public 멤버 함수)

참고 사항

이 예외 유형의 목적은 오류 조건 std::errc::invalid_argument ( std::system_error 에서 std::thread 의 멤버 함수들로부터 throw됨) 및 관련 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 객체와
동일함