Skip to content

C++:noexcept

noexcept specifier

함수가 예외를 던질 수 있는지 아닌지를 지정한다.

Syntax:

noexcept // (1)
noexcept(expression) // (2)
  • 1번은 noexcept(true)와 같다.
  • 2번은 expression이 true으로 평가되면, 함수는 어떠한 예외도 던지지 않는다고 선언된다.

expression은 아래와 같이 선택할 수 있다.

  • constant expression
  • contextually convertible to bool

noexcept operator

Syntax:

noexcept( expression )

noexcept 연산자는 표현식의 noexcept여부를 검사하여 bool 값을 컴파일 타임에 반환한다. 함수 템플릿의 noexcept 지정자 내에서 함수가 일부 유형에는 예외를 발생시키고 다른 유형에는 예외를 발생 시키도록 선언하는 데 사용할 수 있습니다.

noexcept(noexcept(T()))

  • 내부의 noexcept(T())는 operator로써, noexcept 여부를 평가하여 bool값으로 반환한다.
  • 이를 감싸는 외부 noexcept는 specifier로서 noexcep를 지정한다.

noexcept((T()))

Source Code

Output

constexpr static bool __test_true_func() { return true; }
constexpr static bool __test_false_func() { return false; }

static void __test_noexcept_func1() noexcept    { }
static void __test_noexcept_func3() noexcept(1) { }
static void __test_noexcept_func5() noexcept((1)) { }
static void __test_noexcept_func6() noexcept(__test_true_func) { }
static void __test_noexcept_func7() noexcept((__test_true_func)) { }
static void __test_noexcept_func8() noexcept(__test_true_func()) { }
static void __test_noexcept_func9() noexcept((__test_true_func())) { }
static void __test_noexcept_func10() noexcept(__test_false_func) { }
static void __test_noexcept_func11() noexcept((__test_false_func)) { }

static void __test_noexcept_func2() noexcept(0) { }
static void __test_noexcept_func4() noexcept((0)) { }
static void __test_noexcept_func12() noexcept(__test_false_func()) { }
static void __test_noexcept_func13() noexcept((__test_false_func())) { }

#define TBAG_TEST_NOEXCEPT_IMPL(func) \
    std::cout << "Test: " << #func << " -> " << noexcept(func) << std::endl; \
    std::cout << "Test: " << #func << "() -> " << noexcept(func()) << std::endl; \
    std::cout << "Test: noexcept(" << #func << ") -> " << noexcept(noexcept(func)) << std::endl; \
    std::cout << "Test: noexcept(" << #func << "()) -> " << noexcept(noexcept(func())) << std::endl;

TEST(BasicTest, Noexcept)
{
    using namespace std;
    std::cout.setf(std::ios_base::boolalpha);

    // true -> true -> true -> true
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func1);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func3);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func5);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func6);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func7);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func8);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func9);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func10);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func11);

    // true -> false -> true -> true
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func2);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func4);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func12);
    TBAG_TEST_NOEXCEPT_IMPL(__test_noexcept_func13);
}
Test: __test_noexcept_func1 -> true
Test: __test_noexcept_func1() -> true
Test: noexcept(__test_noexcept_func1) -> true
Test: noexcept(__test_noexcept_func1()) -> true
Test: __test_noexcept_func3 -> true
Test: __test_noexcept_func3() -> true
Test: noexcept(__test_noexcept_func3) -> true
Test: noexcept(__test_noexcept_func3()) -> true
Test: __test_noexcept_func5 -> true
Test: __test_noexcept_func5() -> true
Test: noexcept(__test_noexcept_func5) -> true
Test: noexcept(__test_noexcept_func5()) -> true
Test: __test_noexcept_func6 -> true
Test: __test_noexcept_func6() -> true
Test: noexcept(__test_noexcept_func6) -> true
Test: noexcept(__test_noexcept_func6()) -> true
Test: __test_noexcept_func7 -> true
Test: __test_noexcept_func7() -> true
Test: noexcept(__test_noexcept_func7) -> true
Test: noexcept(__test_noexcept_func7()) -> true
Test: __test_noexcept_func8 -> true
Test: __test_noexcept_func8() -> true
Test: noexcept(__test_noexcept_func8) -> true
Test: noexcept(__test_noexcept_func8()) -> true
Test: __test_noexcept_func9 -> true
Test: __test_noexcept_func9() -> true
Test: noexcept(__test_noexcept_func9) -> true
Test: noexcept(__test_noexcept_func9()) -> true
Test: __test_noexcept_func10 -> true
Test: __test_noexcept_func10() -> true
Test: noexcept(__test_noexcept_func10) -> true
Test: noexcept(__test_noexcept_func10()) -> true
Test: __test_noexcept_func11 -> true
Test: __test_noexcept_func11() -> true
Test: noexcept(__test_noexcept_func11) -> true
Test: noexcept(__test_noexcept_func11()) -> true
Test: __test_noexcept_func2 -> true
Test: __test_noexcept_func2() -> false
Test: noexcept(__test_noexcept_func2) -> true
Test: noexcept(__test_noexcept_func2()) -> true
Test: __test_noexcept_func4 -> true
Test: __test_noexcept_func4() -> false
Test: noexcept(__test_noexcept_func4) -> true
Test: noexcept(__test_noexcept_func4()) -> true
Test: __test_noexcept_func12 -> true
Test: __test_noexcept_func12() -> false
Test: noexcept(__test_noexcept_func12) -> true
Test: noexcept(__test_noexcept_func12()) -> true
Test: __test_noexcept_func13 -> true
Test: __test_noexcept_func13() -> false
Test: noexcept(__test_noexcept_func13) -> true
Test: noexcept(__test_noexcept_func13()) -> true

See also

Favorite site