상세 컨텐츠

본문 제목

[C++11] Noexcept 지정자

C++/Modern C++(11, 14, 17, 20)

by deulee 2023. 8. 23. 18:21

본문

`noexcept` 지정자는 C++11로부터 도입된 키워드로, 함수나 연산자가 예외(exception)를 던지지 않음을 나타내는 데 사용된다.

 

이는 기존의 `throw()`의 업그레이드 버전이라고 생각하면 된다.

void func1() noexcept; // do not throw
void func2() noexcept(true); // do not throw
void func3() throw(); // do not throw

void func4() noexcept(false); // may throw

단, `noexcept`를 사용할 때 주의해야 할 점은 만약 이와 같이 선언된 함수에서 예외가 발생하면 `std::terminate`가 호출되어 프로그램이 비정상적으로 종료될 수 있다.

 

이는 `throw()`와 마찬가지로 다음과 같은 구문을 막지 않는다.

#include <iostream>

void f2(void) throw(int)
{
	throw 3;
}

void f1(void) noexcept
{
	f2();
}

int main(void)
{
	f1();
	return 0;
}

관련글 더보기