aboutsummaryrefslogtreecommitdiff
path: root/test/std/utilities/meta/meta.rel/is_nothrow_callable.pass.cpp
blob: eefa6d1f22b18f788ddb7ff71ea8be07c6f1a01a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03, c++11, c++14

// type_traits

// is_nothrow_callable

#include <type_traits>
#include <functional>

#include "test_macros.h"

struct Tag {};

struct Implicit {
  Implicit(int) noexcept {}
};

struct ThrowsImplicit {
  ThrowsImplicit(int) {}
};

struct Explicit {
  explicit Explicit(int) noexcept {}
};

template <bool IsNoexcept, class Ret, class ...Args>
struct CallObject {
  Ret operator()(Args&&...) const noexcept(IsNoexcept);
};

template <class Fn>
constexpr bool throws_callable() {
    return std::is_callable<Fn>::value &&
        !std::is_nothrow_callable<Fn>::value;
}

template <class Fn, class Ret>
constexpr bool throws_callable() {
    return std::is_callable<Fn, Ret>::value &&
        !std::is_nothrow_callable<Fn, Ret>::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_callable will always
        // return false because 'noexcept' is not part of the function type.
        static_assert(throws_callable<decltype(&Dummy::foo)(Dummy&)>(), "");
        static_assert(throws_callable<decltype(&Dummy::bar)()>(), "");
    }
#else
    {
        // Check that PMF's and function pointers actually work and that
        // is_nothrow_callable returns true for noexcept PMF's and function
        // pointers.
        static_assert(std::is_nothrow_callable<decltype(&Dummy::foo)(Dummy&)>::value, "");
        static_assert(std::is_nothrow_callable<decltype(&Dummy::bar)()>::value, "");
    }
#endif
}

int main()
{
    {
        // Check that the conversion to the return type is properly checked
        using Fn = CallObject<true, int>;
        static_assert(std::is_nothrow_callable<Fn(), Implicit>::value, "");
        static_assert(std::is_nothrow_callable<Fn(), double>::value, "");
        static_assert(std::is_nothrow_callable<Fn(), const volatile void>::value, "");
        static_assert(throws_callable<Fn(), ThrowsImplicit>(), "");
        static_assert(!std::is_nothrow_callable<Fn(), Explicit>(), "");
    }
    {
        // Check that the conversion to the parameters is properly checked
        using Fn = CallObject<true, void, const Implicit&, const ThrowsImplicit&>;
        static_assert(std::is_nothrow_callable<Fn(Implicit&, ThrowsImplicit&)>::value, "");
        static_assert(std::is_nothrow_callable<Fn(int, ThrowsImplicit&)>::value, "");
        static_assert(throws_callable<Fn(int, int)>(), "");
        static_assert(!std::is_nothrow_callable<Fn()>::value, "");
    }
    {
        // Check that the noexcept-ness of function objects is checked.
        using Fn = CallObject<true, void>;
        using Fn2 = CallObject<false, void>;
        static_assert(std::is_nothrow_callable<Fn()>::value, "");
        static_assert(throws_callable<Fn2()>(), "");
    }
    {
        // Check that PMD derefs are noexcept
        using Fn = int (Tag::*);
        static_assert(std::is_nothrow_callable<Fn(Tag&)>::value, "");
        static_assert(std::is_nothrow_callable<Fn(Tag&), Implicit>::value, "");
        static_assert(throws_callable<Fn(Tag&), ThrowsImplicit>(), "");
    }
    {
        // Check for is_nothrow_callable_v
        using Fn = CallObject<true, int>;
        static_assert(std::is_nothrow_callable_v<Fn()>, "");
        static_assert(!std::is_nothrow_callable_v<Fn(int)>, "");
    }
    test_noexcept_function_pointers();
}