Skip to content
Search

C++:noexcept

lastmod 2018-08-17created 2015-06-189 edits

noexcept specifier

Specifies whether a function can throw exceptions or not.

Syntax:

noexcept // (1)
noexcept(expression) // (2)
  • (1) is the same as noexcept(true).
  • (2): if expression evaluates to true, the function is declared not to throw any exceptions.

expression can be one of the following.

  • constant expression
  • contextually convertible to bool

noexcept operator

Syntax:

noexcept( expression )

The noexcept operator checks whether an expression is noexcept and returns a bool value at compile time. It can be used within the noexcept specifier of a function template to declare that the function throws exceptions for some types and throws exceptions for other types.

noexcept(noexcept(T()))

  • The inner noexcept(T()) is the operator; it evaluates whether the expression is noexcept and returns a bool value.
  • The outer noexcept wrapping it is the specifier and specifies 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