std:: overflow_error
|
헤더 파일에 정의됨
<stdexcept>
|
||
|
class
overflow_error
;
|
||
예외로 던져질 객체의 유형을 정의합니다. 산술 오버플로 오류(즉, 계산 결과가 대상 유형에 비해 너무 큰 상황)를 보고하는 데 사용할 수 있습니다.
|
이 예외를 던지는 유일한 표준 라이브러리 구성 요소는 std::bitset::to_ulong 입니다. |
(C++11까지) |
|
이 예외를 던지는 유일한 표준 라이브러리 구성 요소들은 std::bitset::to_ulong 과 std::bitset::to_ullong 입니다. |
(C++11부터) |
표준 라이브러리 구성 요소의 수학 함수들은 이 예외를 던지지 않습니다(수학 함수들은
math_errhandling
에 명시된 대로 오버플로 오류를 보고합니다). 그러나 타사 라이브러리들은 이를 사용합니다. 예를 들어,
boost.math
은
std::overflow_error
를 던집니다. 만약
boost::math::policies::throw_on_error
가 활성화된 경우(기본 설정).
std::overflow_error의 모든 멤버 함수는
std::overflow_error
constexpr
입니다: 상수 표현식 평가에서
std::overflow_error
객체를 생성하고 사용하는 것이 가능합니다.
그러나,
|
(C++26부터) |
상속 다이어그램
목차 |
멤버 함수
|
(생성자)
|
주어진 메시지로 새로운
overflow_error
객체를 생성합니다
(public 멤버 함수) |
|
operator=
|
overflow_error
객체를 대체합니다
(public 멤버 함수) |
std::overflow_error:: overflow_error
|
overflow_error
(
const
std::
string
&
what_arg
)
;
|
(1) | (constexpr since C++26) |
|
overflow_error
(
const
char
*
what_arg
)
;
|
(2) | (constexpr since C++26) |
|
overflow_error
(
const
overflow_error
&
other
)
;
|
(3) |
(noexcept since C++11)
(constexpr since C++26) |
std::overflow_error
를 가지면
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
입니다. 복사 생성자에서는 예외가 발생할 수 없습니다.
매개변수
| what_arg | - | 설명 문자열 |
| other | - | 복사할 다른 예외 객체 |
예외
참고
std::overflow_error
의 복사가 예외를 발생시키는 것이 허용되지 않기 때문에, 이 메시지는 일반적으로 별도로 할당된 참조 카운트 문자열로 내부에 저장됩니다. 이것이
std::string&&
를 취하는 생성자가 없는 이유이기도 합니다: 어차피 내용을 복사해야 하기 때문입니다.
LWG issue 254 의 해결 이전에는, 비복사 생성자는 std::string 만 받아들일 수 있었습니다. 이는 std::string 객체를 생성하기 위해 동적 할당을 필수적으로 만들었습니다.
LWG issue 471
의 해결 이후, 파생된 표준 예외 클래스는 공개적으로 접근 가능한 복사 생성자를 가져야 합니다. 원본 객체와 복사된 객체에 대해
what()
으로 얻은 설명 문자열이 동일한 한 암시적으로 정의될 수 있습니다.
std::overflow_error:: operator=
|
overflow_error
&
operator
=
(
const
overflow_error
&
other
)
;
|
(noexcept since C++11)
(constexpr since C++26) |
|
other
의 내용으로 내용을 할당합니다.
*
this
와
other
모두 동적 타입
std::overflow_error
를 가진다면, 할당 후
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
입니다. 복사 할당 연산자에서는 예외가 발생할 수 없습니다.
매개변수
| other | - | 할당할 다른 예외 객체 |
반환값
* this
참고
LWG 이슈 471
의 해결 이후, 파생된 표준 예외 클래스는 공개적으로 접근 가능한 복사 할당 연산자를 가져야 합니다.
what()
로 얻은 설명 문자열이 원본 객체와 복사된 객체에 대해 동일한 경우 암시적으로 정의될 수 있습니다.
std:: runtime_error 에서 상속됨
std:: exception 로부터 상속됨
멤버 함수
|
[virtual]
|
예외 객체를 파괴함
(
std::exception
의 virtual public 멤버 함수)
|
|
[virtual]
|
설명 문자열을 반환함
(
std::exception
의 virtual public 멤버 함수)
|
참고 사항
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_constexpr_exceptions
|
202502L
|
(C++26) | constexpr 예외 타입 |
예제
#include <iostream> #include <limits> #include <stdexcept> #include <utility> template<typename T, int N> requires (N > 0) /*...*/ class Stack { int top_{-1}; T data_[N]; public: [[nodiscard]] bool empty() const { return top_ == -1; } void push(T x) { if (top_ == N - 1) throw std::overflow_error("Stack overflow!"); data_[++top_] = std::move(x); } void pop() { if (empty()) throw std::underflow_error("Stack underflow!"); --top_; } T const& top() const { if (empty()) throw std::overflow_error("Stack is empty!"); return data_[top_]; } }; int main() { Stack<int, 4> st; try { [[maybe_unused]] auto x = st.top(); } catch (std::overflow_error const& ex) { std::cout << "1) Exception: " << ex.what() << '\n'; } st.push(1337); while (!st.empty()) st.pop(); try { st.pop(); } catch (std::underflow_error const& ex) { std::cout << "2) Exception: " << ex.what() << '\n'; } try { for (int i{}; i != 13; ++i) st.push(i); } catch (std::overflow_error const& ex) { std::cout << "3) Exception: " << ex.what() << '\n'; } }
출력:
1) Exception: Stack is empty! 2) Exception: Stack underflow! 3) Exception: Stack overflow!
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 254 | C++98 | const char * 를 받는 생성자가 누락됨 | 추가됨 |
| LWG 471 | C++98 |
std::overflow_error
의 설명 문자열 사본이
구현에 따라 정의됨 |
원본
std::overflow_error
객체와
동일함 |