File: function_ref_mfn_test.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (179 lines) | stat: -rw-r--r-- 5,789 bytes parent folder | download | duplicates (2)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2024 Christian Mazakas
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#if defined(__GNUC__) && __GNUC__ == 7
#pragma GCC diagnostic ignored "-Wnoexcept-type"
#endif

#include <boost/compat/function_ref.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <memory>
#include <type_traits>

#if defined(__cpp_nontype_template_parameter_auto) && __cpp_nontype_template_parameter_auto >= 201606L

#define NONTYPE(fn) compat::nontype<fn>
#define NONTYPE_T(fn) compat::nontype_t<fn>

#else

#define NONTYPE(fn) compat::detail::nttp_holder<decltype(fn), fn>{}
#define NONTYPE_T(fn) compat::detail::nttp_holder<decltype(fn), fn>

#endif

struct F1 {
  std::unique_ptr<int> p_;
  int q_ = 1337;

  F1() : p_(new int(1)) {}

  int m1() { return *p_ + -1; }
  int m2(int x1) noexcept { return 10 * *p_ + x1; }
  int m3(int x1, int x2) const { return 100 * *p_ + 10 * x1 + x2; }
  int m4(int x1, int x2, int x3) const noexcept { return 1000 * *p_ + 100 * x1 + 10 * x2 + x3; }
};

namespace compat = boost::compat;

int main() {
  {
    F1 f1;

    compat::function_ref<int(F1&)> fn1(NONTYPE(&F1::m1));
    BOOST_TEST_EQ(fn1(f1), 0);

    int x = 2;
    compat::function_ref<int(F1&, int)> fn2(NONTYPE(&F1::m2));
    BOOST_TEST_EQ(fn2(f1, x), 12);

    compat::function_ref<int(F1&, int, int)> fn3(NONTYPE(&F1::m3));
    BOOST_TEST_EQ(fn3(f1, 2, 3), 123);

    compat::function_ref<int(F1&, int, int, int)> fn4(NONTYPE(&F1::m4));
    BOOST_TEST_EQ(fn4(f1, 2, 3, 4), 1234);

    compat::function_ref<int(F1&)> a1(NONTYPE(&F1::q_));
    BOOST_TEST_EQ(a1(f1), 1337);
  }

  {
    F1 f1;

    compat::function_ref<int(F1&) const> fn1(NONTYPE(&F1::m1));
    BOOST_TEST_EQ(fn1(f1), 0);

    compat::function_ref<int(F1&, int) const> fn2(NONTYPE(&F1::m2));
    BOOST_TEST_EQ(fn2(f1, 2), 12);

    compat::function_ref<int(F1&, int, int) const> fn3(NONTYPE(&F1::m3));
    BOOST_TEST_EQ(fn3(f1, 2, 3), 123);

    compat::function_ref<int(F1&, int, int, int) const> fn4(NONTYPE(&F1::m4));
    BOOST_TEST_EQ(fn4(f1, 2, 3, 4), 1234);
  }

  {
    BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<int(F1 const&)>, NONTYPE_T(&F1::m1)>));
    BOOST_TEST_TRAIT_FALSE(
        (std::is_constructible<compat::function_ref<int(F1 const&, int)>, NONTYPE_T(&F1::m2)>));

    F1 f1;

    compat::function_ref<int(F1 const&, int, int) const> fn3(NONTYPE(&F1::m3));
    BOOST_TEST_EQ(fn3(f1, 2, 3), 123);

    compat::function_ref<int(F1 const&, int, int, int) const> fn4(NONTYPE(&F1::m4));
    BOOST_TEST_EQ(fn4(f1, 2, 3, 4), 1234);

    auto const& f2 = f1;

    compat::function_ref<int(F1 const&, int, int) const> fn23(NONTYPE(&F1::m3));
    BOOST_TEST_EQ(fn3(f2, 2, 3), 123);

    compat::function_ref<int(F1 const&, int, int, int) const> fn24(NONTYPE(&F1::m4));
    BOOST_TEST_EQ(fn4(f2, 2, 3, 4), 1234);
  }

  {
    BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<int(int)>, NONTYPE_T(&F1::m2), F1&>));
    BOOST_TEST_TRAIT_FALSE(
        (std::is_constructible<compat::function_ref<int(int)>, NONTYPE_T(&F1::m2), F1 const&>));
    BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<int(int)>, NONTYPE_T(&F1::m2), F1&&>));
    BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<int(int)>, NONTYPE_T(&F1::m2), F1>));

    BOOST_TEST_TRAIT_TRUE(
        (std::is_constructible<compat::function_ref<int(int, int) const>, NONTYPE_T(&F1::m3), F1&>));
    BOOST_TEST_TRAIT_TRUE(
        (std::is_constructible<compat::function_ref<int(int, int) const>, NONTYPE_T(&F1::m3), F1 const&>));
    BOOST_TEST_TRAIT_FALSE(
        (std::is_constructible<compat::function_ref<int(int, int) const>, NONTYPE_T(&F1::m3), F1&&>));
    BOOST_TEST_TRAIT_FALSE(
        (std::is_constructible<compat::function_ref<int(int, int) const>, NONTYPE_T(&F1::m3), F1>));
  }

  {
    F1 f1;

    compat::function_ref<int()> fn1(NONTYPE(&F1::m1), f1);
    BOOST_TEST_EQ(fn1(), 0);

    compat::function_ref<int(int)> fn2(NONTYPE(&F1::m2), f1);
    BOOST_TEST_EQ(fn2(2), 12);

    compat::function_ref<int(int, int)> fn3(NONTYPE(&F1::m3), f1);
    BOOST_TEST_EQ(fn3(2, 3), 123);

    compat::function_ref<int(int, int, int)> fn4(NONTYPE(&F1::m4), f1);
    BOOST_TEST_EQ(fn4(2, 3, 4), 1234);
  }

  {
    F1 const f1;

    BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<int() const>, NONTYPE_T(&F1::m1), F1&>));
    BOOST_TEST_TRAIT_FALSE(
        (std::is_constructible<compat::function_ref<int(int) const>, NONTYPE_T(&F1::m2), F1&>));

    compat::function_ref<int(int, int) const> fn3(NONTYPE(&F1::m3), f1);
    BOOST_TEST_EQ(fn3(2, 3), 123);

    compat::function_ref<int(int, int, int) const> fn4(NONTYPE(&F1::m4), f1);
    BOOST_TEST_EQ(fn4(2, 3, 4), 1234);
  }

  {
    F1 f1;

    compat::function_ref<int()> fn1(NONTYPE(&F1::m1), &f1);
    BOOST_TEST_EQ(fn1(), 0);

    compat::function_ref<int(int)> fn2(NONTYPE(&F1::m2), &f1);
    BOOST_TEST_EQ(fn2(2), 12);

    compat::function_ref<int(int, int)> fn3(NONTYPE(&F1::m3), &f1);
    BOOST_TEST_EQ(fn3(2, 3), 123);

    compat::function_ref<int(int, int, int)> fn4(NONTYPE(&F1::m4), &f1);
    BOOST_TEST_EQ(fn4(2, 3, 4), 1234);
  }

  {
    F1 const f1;

    BOOST_TEST_TRAIT_FALSE(
        (std::is_constructible<compat::function_ref<int() const>, NONTYPE_T(&F1::m1), F1 const*>));
    BOOST_TEST_TRAIT_FALSE(
        (std::is_constructible<compat::function_ref<int(int) const>, NONTYPE_T(&F1::m2), F1 const*>));

    compat::function_ref<int(int, int) const> fn3(NONTYPE(&F1::m3), &f1);
    BOOST_TEST_EQ(fn3(2, 3), 123);

    compat::function_ref<int(int, int, int) const> fn4(NONTYPE(&F1::m4), &f1);
    BOOST_TEST_EQ(fn4(2, 3, 4), 1234);
  }

  return boost::report_errors();
}