//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++98, c++03, c++11, c++14 // type_traits // is_nothrow_invocable #include #include #include "test_macros.h" struct Tag {}; struct Implicit { Implicit(int) noexcept {} }; struct ThrowsImplicit { ThrowsImplicit(int) {} }; struct Explicit { explicit Explicit(int) noexcept {} }; template struct CallObject { Ret operator()(Args&&...) const noexcept(IsNoexcept); }; template constexpr bool throws_invocable() { return std::is_invocable::value && !std::is_nothrow_invocable::value; } template constexpr bool throws_invocable_r() { return std::is_invocable_r::value && !std::is_nothrow_invocable_r::value; } // FIXME(EricWF) Don't test the where noexcept is *not* part of the type system // once implementations have caught up. void test_noexcept_function_pointers() { struct Dummy { void foo() noexcept {} static void bar() noexcept {} }; #if !defined(__cpp_noexcept_function_type) { // Check that PMF's and function pointers *work*. is_nothrow_invocable will always // return false because 'noexcept' is not part of the function type. static_assert(throws_invocable(), ""); static_assert(throws_invocable(), ""); } #else { // Check that PMF's and function pointers actually work and that // is_nothrow_invocable returns true for noexcept PMF's and function // pointers. static_assert(std::is_nothrow_invocable::value, ""); static_assert(std::is_nothrow_invocable::value, ""); } #endif } int main() { { // Check that the conversion to the return type is properly checked using Fn = CallObject; static_assert(std::is_nothrow_invocable_r::value, ""); static_assert(std::is_nothrow_invocable_r::value, ""); static_assert(std::is_nothrow_invocable_r::value, ""); static_assert(throws_invocable_r(), ""); static_assert(!std::is_nothrow_invocable(), ""); } { // Check that the conversion to the parameters is properly checked using Fn = CallObject; static_assert(std::is_nothrow_invocable::value, ""); static_assert(std::is_nothrow_invocable::value, ""); static_assert(throws_invocable(), ""); static_assert(!std::is_nothrow_invocable::value, ""); } { // Check that the noexcept-ness of function objects is checked. using Fn = CallObject; using Fn2 = CallObject; static_assert(std::is_nothrow_invocable::value, ""); static_assert(throws_invocable(), ""); } { // Check that PMD derefs are noexcept using Fn = int (Tag::*); static_assert(std::is_nothrow_invocable::value, ""); static_assert(std::is_nothrow_invocable_r::value, ""); static_assert(throws_invocable_r(), ""); } { // Check for is_nothrow_invocable_v using Fn = CallObject; static_assert(std::is_nothrow_invocable_v, ""); static_assert(!std::is_nothrow_invocable_v, ""); } { // Check for is_nothrow_invocable_r_v using Fn = CallObject; static_assert(std::is_nothrow_invocable_r_v, ""); static_assert(!std::is_nothrow_invocable_r_v, ""); } test_noexcept_function_pointers(); }