mirror of
https://github.com/boostorg/compat.git
synced 2026-01-19 16:12:15 +00:00
implement pointer-to-member function_ref overloads
This commit is contained in:
@@ -52,6 +52,9 @@ class function_ref<R(ArgTypes...) cv noexcept(noex)> {
|
||||
public:
|
||||
template<class F> function_ref(F*) noexcept;
|
||||
template<class F> function_ref(F&&) noexcept;
|
||||
template<auto f> function_ref(nontype_t<f>) noexcept;
|
||||
template<auto f, class U> function_ref(nontype_t<f>, U&&) noexcept;
|
||||
template<auto f, class T> function_ref(nontype_t<f>, cv T*) noexcept;
|
||||
|
||||
function_ref(const function_ref&) noexcept = default;
|
||||
function_ref& operator=(const function_ref&) noexcept = default;
|
||||
@@ -92,6 +95,76 @@ participates in resolution when `fn` is not a pointer-to-member or pointer-to-me
|
||||
+
|
||||
Calling the `function_ref` is expression-equivalent to: + `invoke_r<R>(static_cast<cv T&>(f), call-args...)`.
|
||||
|
||||
### Pointer to Member Function Constructor
|
||||
|
||||
```cpp
|
||||
template<auto f> function_ref(nontype_t<f>) noexcept;
|
||||
```
|
||||
|
||||
[horizontal]
|
||||
Effects:;; Constructs a `function_ref` using the supplied pointer to member function. This overload only participates
|
||||
in resolution when `f` is a pointer to member or pointer to member function. +
|
||||
+
|
||||
Calling the `function_ref` is express-equivalent to: `invoke_r<R>(f, class-args)`.
|
||||
Example:;;
|
||||
+
|
||||
--
|
||||
```cpp
|
||||
struct point { int x = 1, y = 2; };
|
||||
|
||||
point p;
|
||||
compat::function_ref<int(point const&)> f(compat::nontype_t<&point::x>{});
|
||||
|
||||
BOOST_TEST_EQ(f(p), 1);
|
||||
```
|
||||
--
|
||||
|
||||
### Bound Object Constructor
|
||||
|
||||
```cpp
|
||||
template<auto f, class U> function_ref(nontype_t<f>, U&&) noexcept;
|
||||
```
|
||||
|
||||
[horizontal]
|
||||
Effects:;; Constructs a `function_ref` using the supplied pointer to member or pointer to member function and a reference
|
||||
to an object to bind it to. +
|
||||
+
|
||||
This overload only participates in resolution if `is_rvalue_reference_v<U&&>` is false.
|
||||
Example:;;
|
||||
+
|
||||
--
|
||||
```cpp
|
||||
struct point { int x = 1, y = 2; };
|
||||
|
||||
point p;
|
||||
compat::function_ref<int()> f(compat::nontype_t<&point::x>{}, p);
|
||||
|
||||
BOOST_TEST_EQ(f(), 1);
|
||||
```
|
||||
--
|
||||
|
||||
### Bound Pointer Constructor
|
||||
|
||||
```cpp
|
||||
template<auto f, class T> function_ref(nontype_t<f>, cv T*) noexcept;
|
||||
```
|
||||
|
||||
[horizontal]
|
||||
Effects:;; Constructs a `function_ref` using the supplied pointer to member or pointer to member function and a pointer
|
||||
to an object.
|
||||
Example:;;
|
||||
+
|
||||
--
|
||||
```cpp
|
||||
struct point { int x = 1, y = 2; };
|
||||
|
||||
point p;
|
||||
compat::function_ref<int()> f(compat::nontype_t<&point::x>{}, &p);
|
||||
|
||||
BOOST_TEST_EQ(f(), 1);
|
||||
```
|
||||
--
|
||||
|
||||
### Copy Constructor
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -8,9 +8,12 @@
|
||||
#include <boost/compat/invoke.hpp>
|
||||
#include <boost/compat/type_traits.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#if defined(__cpp_nontype_template_parameter_auto) && __cpp_nontype_template_parameter_auto >= 201606L
|
||||
#define BOOST_COMPAT_HAS_AUTO_NTTP
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace compat {
|
||||
@@ -18,6 +21,15 @@ namespace compat {
|
||||
template <class... S>
|
||||
struct function_ref;
|
||||
|
||||
#if defined(BOOST_COMPAT_HAS_AUTO_NTTP)
|
||||
|
||||
template <auto V>
|
||||
struct nontype_t {
|
||||
explicit nontype_t() = default;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <bool NoEx>
|
||||
@@ -43,6 +55,34 @@ struct invoke_object_holder {
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(BOOST_COMPAT_HAS_AUTO_NTTP)
|
||||
|
||||
template <auto f, bool Const, bool NoEx, class R, class... Args>
|
||||
struct invoke_mem_fn_holder {
|
||||
static R invoke_mem_fn(thunk_storage<NoEx> /* s */, Args&&... args) noexcept(NoEx) {
|
||||
return compat::invoke_r<R>(f, std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <auto f, class U, bool Const, bool NoEx, class R, class... Args>
|
||||
struct invoke_target_mem_fn_holder {
|
||||
static R invoke_mem_fn(thunk_storage<NoEx> s, Args&&... args) noexcept(NoEx) {
|
||||
using T = remove_reference_t<U>;
|
||||
using cv_T = conditional_t<Const, add_const_t<T>, T>;
|
||||
return compat::invoke_r<R>(f, *static_cast<cv_T*>(s.pobj_), std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <auto f, class T, bool Const, bool NoEx, class R, class... Args>
|
||||
struct invoke_ptr_mem_fn_holder {
|
||||
static R invoke_mem_fn(thunk_storage<NoEx> s, Args&&... args) noexcept(NoEx) {
|
||||
using cv_T = conditional_t<Const, add_const_t<T>, T>;
|
||||
return compat::invoke_r<R>(f, static_cast<cv_T*>(s.pobj_), std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
template <bool Const, bool NoEx, class R, class... Args>
|
||||
struct function_ref_base {
|
||||
private:
|
||||
@@ -52,6 +92,7 @@ private:
|
||||
public:
|
||||
struct fp_tag {};
|
||||
struct obj_tag {};
|
||||
struct mem_fn_tag {};
|
||||
|
||||
template <class F>
|
||||
function_ref_base(fp_tag, F* fn) noexcept
|
||||
@@ -65,6 +106,28 @@ public:
|
||||
thunk_.pobj_ = const_cast<void*>(static_cast<void const*>(std::addressof(fn)));
|
||||
}
|
||||
|
||||
#if defined(BOOST_COMPAT_HAS_AUTO_NTTP)
|
||||
|
||||
template <auto f>
|
||||
function_ref_base(mem_fn_tag, nontype_t<f>)
|
||||
: thunk_{}, invoke_(&invoke_mem_fn_holder<f, Const, NoEx, R, Args...>::invoke_mem_fn) {
|
||||
thunk_.pobj_ = nullptr;
|
||||
}
|
||||
|
||||
template <auto f, class U>
|
||||
function_ref_base(mem_fn_tag, nontype_t<f>, U&& obj)
|
||||
: thunk_{}, invoke_(&invoke_target_mem_fn_holder<f, U, Const, NoEx, R, Args...>::invoke_mem_fn) {
|
||||
thunk_.pobj_ = const_cast<void*>(static_cast<void const*>(std::addressof(obj)));
|
||||
}
|
||||
|
||||
template <auto f, class T>
|
||||
function_ref_base(mem_fn_tag, nontype_t<f>, T* obj)
|
||||
: thunk_{}, invoke_(&invoke_ptr_mem_fn_holder<f, T, Const, NoEx, R, Args...>::invoke_mem_fn) {
|
||||
thunk_.pobj_ = const_cast<void*>(static_cast<void const*>(obj));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
function_ref_base(const function_ref_base&) noexcept = default;
|
||||
function_ref_base& operator=(const function_ref_base&) noexcept = default;
|
||||
|
||||
@@ -78,6 +141,7 @@ struct function_ref<R(Args...)> : public detail::function_ref_base<false, false,
|
||||
private:
|
||||
using base_type = detail::function_ref_base<false, false, R, Args...>;
|
||||
using typename base_type::fp_tag;
|
||||
using typename base_type::mem_fn_tag;
|
||||
using typename base_type::obj_tag;
|
||||
|
||||
template <class... T>
|
||||
@@ -93,6 +157,20 @@ public:
|
||||
int> = 0>
|
||||
function_ref(F&& fn) noexcept : base_type(obj_tag{}, fn) {}
|
||||
|
||||
#if defined(BOOST_COMPAT_HAS_AUTO_NTTP)
|
||||
|
||||
template <auto f, class F = decltype(f), enable_if_t<is_invocable_using<F>::value, int> = 0>
|
||||
function_ref(nontype_t<f>) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}) {}
|
||||
|
||||
template <auto f, class U, class T = remove_reference_t<U>, class F = decltype(f),
|
||||
enable_if_t<!std::is_rvalue_reference_v<U&&> && is_invocable_using<F, T&>::value, int> = 0>
|
||||
function_ref(nontype_t<f>, U&& obj) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}, std::forward<U>(obj)) {}
|
||||
|
||||
template <auto f, class T, class F = decltype(f), enable_if_t<is_invocable_using<F, T*>::value, int> = 0>
|
||||
function_ref(nontype_t<f>, T* obj) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}, obj) {}
|
||||
|
||||
#endif
|
||||
|
||||
function_ref(const function_ref&) noexcept = default;
|
||||
function_ref& operator=(const function_ref&) noexcept = default;
|
||||
|
||||
@@ -105,6 +183,7 @@ struct function_ref<R(Args...) const> : public detail::function_ref_base<true, f
|
||||
private:
|
||||
using base_type = detail::function_ref_base<true, false, R, Args...>;
|
||||
using typename base_type::fp_tag;
|
||||
using typename base_type::mem_fn_tag;
|
||||
using typename base_type::obj_tag;
|
||||
|
||||
template <class... T>
|
||||
@@ -120,6 +199,20 @@ public:
|
||||
int> = 0>
|
||||
function_ref(F&& fn) noexcept : base_type(obj_tag{}, fn) {}
|
||||
|
||||
#if defined(BOOST_COMPAT_HAS_AUTO_NTTP)
|
||||
|
||||
template <auto f, class F = decltype(f), enable_if_t<is_invocable_using<F>::value, int> = 0>
|
||||
function_ref(nontype_t<f>) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}) {}
|
||||
|
||||
template <auto f, class U, class T = remove_reference_t<U>, class F = decltype(f),
|
||||
enable_if_t<!std::is_rvalue_reference_v<U&&> && is_invocable_using<F, T const&>::value, int> = 0>
|
||||
function_ref(nontype_t<f>, U&& obj) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}, std::forward<U>(obj)) {}
|
||||
|
||||
template <auto f, class T, class F = decltype(f), enable_if_t<is_invocable_using<F, T const*>::value, int> = 0>
|
||||
function_ref(nontype_t<f>, T const* obj) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}, obj) {}
|
||||
|
||||
#endif
|
||||
|
||||
function_ref(const function_ref&) noexcept = default;
|
||||
function_ref& operator=(const function_ref&) noexcept = default;
|
||||
|
||||
@@ -134,6 +227,7 @@ struct function_ref<R(Args...) noexcept> : public detail::function_ref_base<fals
|
||||
private:
|
||||
using base_type = detail::function_ref_base<false, true, R, Args...>;
|
||||
using typename base_type::fp_tag;
|
||||
using typename base_type::mem_fn_tag;
|
||||
using typename base_type::obj_tag;
|
||||
|
||||
template <class... T>
|
||||
@@ -149,6 +243,20 @@ public:
|
||||
int> = 0>
|
||||
function_ref(F&& fn) noexcept : base_type(obj_tag{}, fn) {}
|
||||
|
||||
#if defined(BOOST_COMPAT_HAS_AUTO_NTTP)
|
||||
|
||||
template <auto f, class F = decltype(f), enable_if_t<is_invocable_using<F>::value, int> = 0>
|
||||
function_ref(nontype_t<f>) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}) {}
|
||||
|
||||
template <auto f, class U, class T = remove_reference_t<U>, class F = decltype(f),
|
||||
enable_if_t<!std::is_rvalue_reference_v<U&&> && is_invocable_using<F, T&>::value, int> = 0>
|
||||
function_ref(nontype_t<f>, U&& obj) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}, std::forward<U>(obj)) {}
|
||||
|
||||
template <auto f, class T, class F = decltype(f), enable_if_t<is_invocable_using<F, T*>::value, int> = 0>
|
||||
function_ref(nontype_t<f>, T* obj) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}, obj) {}
|
||||
|
||||
#endif
|
||||
|
||||
function_ref(const function_ref&) noexcept = default;
|
||||
function_ref& operator=(const function_ref&) noexcept = default;
|
||||
|
||||
@@ -161,6 +269,7 @@ struct function_ref<R(Args...) const noexcept> : public detail::function_ref_bas
|
||||
private:
|
||||
using base_type = detail::function_ref_base<true, true, R, Args...>;
|
||||
using typename base_type::fp_tag;
|
||||
using typename base_type::mem_fn_tag;
|
||||
using typename base_type::obj_tag;
|
||||
|
||||
template <class... T>
|
||||
@@ -176,6 +285,20 @@ public:
|
||||
int> = 0>
|
||||
function_ref(F&& fn) noexcept : base_type(obj_tag{}, fn) {}
|
||||
|
||||
#if defined(BOOST_COMPAT_HAS_AUTO_NTTP)
|
||||
|
||||
template <auto f, class F = decltype(f), enable_if_t<is_invocable_using<F>::value, int> = 0>
|
||||
function_ref(nontype_t<f>) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}) {}
|
||||
|
||||
template <auto f, class U, class T = remove_reference_t<U>, class F = decltype(f),
|
||||
enable_if_t<!std::is_rvalue_reference_v<U&&> && is_invocable_using<F, T const&>::value, int> = 0>
|
||||
function_ref(nontype_t<f>, U&& obj) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}, std::forward<U>(obj)) {}
|
||||
|
||||
template <auto f, class T, class F = decltype(f), enable_if_t<is_invocable_using<F, T const*>::value, int> = 0>
|
||||
function_ref(nontype_t<f>, T const* obj) noexcept : base_type(mem_fn_tag{}, nontype_t<f>{}, obj) {}
|
||||
|
||||
#endif
|
||||
|
||||
function_ref(const function_ref&) noexcept = default;
|
||||
function_ref& operator=(const function_ref&) noexcept = default;
|
||||
|
||||
|
||||
@@ -103,6 +103,8 @@ run is_nothrow_invocable_r_test.cpp ;
|
||||
|
||||
run function_ref_fn_test.cpp ;
|
||||
run function_ref_obj_test.cpp ;
|
||||
run function_ref_mfn_test.cpp ;
|
||||
|
||||
run function_ref_fn_noexcept_test.cpp ;
|
||||
run function_ref_mfn_noexcept_test.cpp ;
|
||||
run function_ref_obj_noexcept_test.cpp ;
|
||||
|
||||
164
test/function_ref_mfn_noexcept_test.cpp
Normal file
164
test/function_ref_mfn_noexcept_test.cpp
Normal file
@@ -0,0 +1,164 @@
|
||||
// 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>
|
||||
|
||||
#if !defined(BOOST_COMPAT_HAS_AUTO_NTTP)
|
||||
#include <boost/config/pragma_message.hpp>
|
||||
BOOST_PRAGMA_MESSAGE("no support for placeholder NTTPs detected, skipping this test")
|
||||
int main() {}
|
||||
#else
|
||||
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/core/lightweight_test_trait.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
struct F1 {
|
||||
std::unique_ptr<int> p_;
|
||||
|
||||
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() {
|
||||
{
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(F1&) noexcept>, compat::nontype_t<&F1::m1>>));
|
||||
BOOST_TEST_TRAIT_TRUE(
|
||||
(std::is_constructible<compat::function_ref<int(F1&, int) noexcept>, compat::nontype_t<&F1::m2>>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(F1&, int, int) noexcept>, compat::nontype_t<&F1::m3>>));
|
||||
BOOST_TEST_TRAIT_TRUE(
|
||||
(std::is_constructible<compat::function_ref<int(F1&, int, int, int) noexcept>, compat::nontype_t<&F1::m4>>));
|
||||
}
|
||||
|
||||
{
|
||||
F1 f1;
|
||||
|
||||
compat::function_ref<int(F1&, int) noexcept> fn2(compat::nontype_t<&F1::m2>{});
|
||||
BOOST_TEST_EQ(fn2(f1, 2), 12);
|
||||
|
||||
compat::function_ref<int(F1&, int, int, int) noexcept> fn4(compat::nontype_t<&F1::m4>{});
|
||||
BOOST_TEST_EQ(fn4(f1, 2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(F1&) const noexcept>, compat::nontype_t<&F1::m1>>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(F1&, int, int) const noexcept>, compat::nontype_t<&F1::m3>>));
|
||||
|
||||
F1 f1;
|
||||
|
||||
compat::function_ref<int(F1&, int) const noexcept> fn2(compat::nontype_t<&F1::m2>{});
|
||||
BOOST_TEST_EQ(fn2(f1, 2), 12);
|
||||
|
||||
compat::function_ref<int(F1&, int, int, int) const noexcept> fn4(compat::nontype_t<&F1::m4>{});
|
||||
BOOST_TEST_EQ(fn4(f1, 2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(F1 const&) noexcept>, compat::nontype_t<&F1::m1>>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(F1 const&, int) noexcept>, compat::nontype_t<&F1::m2>>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(F1 const&, int, int) noexcept>, compat::nontype_t<&F1::m3>>));
|
||||
|
||||
F1 f1;
|
||||
|
||||
compat::function_ref<int(F1 const&, int, int, int) noexcept> fn4(compat::nontype_t<&F1::m4>{});
|
||||
BOOST_TEST_EQ(fn4(f1, 2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int() noexcept>, compat::nontype_t<&F1::m1>, F1&>));
|
||||
BOOST_TEST_TRAIT_TRUE(
|
||||
(std::is_constructible<compat::function_ref<int(int) noexcept>, compat::nontype_t<&F1::m2>, F1&>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(int, int) noexcept>, compat::nontype_t<&F1::m3>, F1&>));
|
||||
BOOST_TEST_TRAIT_TRUE(
|
||||
(std::is_constructible<compat::function_ref<int(int, int, int) noexcept>, compat::nontype_t<&F1::m4>, F1&>));
|
||||
|
||||
F1 f1;
|
||||
|
||||
compat::function_ref<int(int) noexcept> fn2(compat::nontype_t<&F1::m2>{}, f1);
|
||||
BOOST_TEST_EQ(fn2(2), 12);
|
||||
|
||||
compat::function_ref<int(int, int, int) noexcept> fn4(compat::nontype_t<&F1::m4>{}, f1);
|
||||
BOOST_TEST_EQ(fn4(2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int() const noexcept>, compat::nontype_t<&F1::m1>, F1 const&>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(int) const noexcept>, compat::nontype_t<&F1::m2>, F1 const&>));
|
||||
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<int(int, int) const noexcept>,
|
||||
compat::nontype_t<&F1::m3>, F1 const&>));
|
||||
|
||||
F1 f1;
|
||||
|
||||
compat::function_ref<int(int, int, int) const noexcept> fn4(compat::nontype_t<&F1::m4>{}, f1);
|
||||
BOOST_TEST_EQ(fn4(2, 3, 4), 1234);
|
||||
|
||||
auto const& f1_2 = f1;
|
||||
compat::function_ref<int(int, int, int) const noexcept> fn4_2(compat::nontype_t<&F1::m4>{}, f1_2);
|
||||
BOOST_TEST_EQ(fn4_2(2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int() noexcept>, compat::nontype_t<&F1::m1>, F1*>));
|
||||
BOOST_TEST_TRAIT_TRUE(
|
||||
(std::is_constructible<compat::function_ref<int(int) noexcept>, compat::nontype_t<&F1::m2>, F1*>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(int, int) noexcept>, compat::nontype_t<&F1::m3>, F1*>));
|
||||
BOOST_TEST_TRAIT_TRUE(
|
||||
(std::is_constructible<compat::function_ref<int(int, int, int) noexcept>, compat::nontype_t<&F1::m4>, F1*>));
|
||||
|
||||
F1 f1;
|
||||
|
||||
compat::function_ref<int(int) noexcept> fn2(compat::nontype_t<&F1::m2>{}, &f1);
|
||||
BOOST_TEST_EQ(fn2(2), 12);
|
||||
|
||||
compat::function_ref<int(int, int, int) noexcept> fn4(compat::nontype_t<&F1::m4>{}, &f1);
|
||||
BOOST_TEST_EQ(fn4(2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int() const noexcept>, compat::nontype_t<&F1::m1>, F1 const*>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(int) const noexcept>, compat::nontype_t<&F1::m2>, F1 const*>));
|
||||
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<int(int, int) const noexcept>,
|
||||
compat::nontype_t<&F1::m3>, F1 const*>));
|
||||
|
||||
F1 const f1;
|
||||
|
||||
compat::function_ref<int(int, int, int) const noexcept> fn4(compat::nontype_t<&F1::m4>{}, &f1);
|
||||
BOOST_TEST_EQ(fn4(2, 3, 4), 1234);
|
||||
|
||||
auto const& f1_2 = f1;
|
||||
compat::function_ref<int(int, int, int) const noexcept> fn4_2(compat::nontype_t<&F1::m4>{}, &f1_2);
|
||||
BOOST_TEST_EQ(fn4_2(2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#endif
|
||||
176
test/function_ref_mfn_test.cpp
Normal file
176
test/function_ref_mfn_test.cpp
Normal file
@@ -0,0 +1,176 @@
|
||||
// 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>
|
||||
|
||||
#if !defined(BOOST_COMPAT_HAS_AUTO_NTTP)
|
||||
#include <boost/config/pragma_message.hpp>
|
||||
BOOST_PRAGMA_MESSAGE("no support for placeholder NTTPs detected, skipping this test")
|
||||
int main() {}
|
||||
#else
|
||||
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/core/lightweight_test_trait.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
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(compat::nontype_t<&F1::m1>{});
|
||||
BOOST_TEST_EQ(fn1(f1), 0);
|
||||
|
||||
compat::function_ref<int(F1&, int)> fn2(compat::nontype_t<&F1::m2>{});
|
||||
BOOST_TEST_EQ(fn2(f1, 2), 12);
|
||||
|
||||
compat::function_ref<int(F1&, int, int)> fn3(compat::nontype_t<&F1::m3>{});
|
||||
BOOST_TEST_EQ(fn3(f1, 2, 3), 123);
|
||||
|
||||
compat::function_ref<int(F1&, int, int, int)> fn4(compat::nontype_t<&F1::m4>{});
|
||||
BOOST_TEST_EQ(fn4(f1, 2, 3, 4), 1234);
|
||||
|
||||
compat::function_ref<int(F1&)> a1(compat::nontype_t<&F1::q_>{});
|
||||
BOOST_TEST_EQ(a1(f1), 1337);
|
||||
}
|
||||
|
||||
{
|
||||
F1 f1;
|
||||
|
||||
compat::function_ref<int(F1&) const> fn1(compat::nontype_t<&F1::m1>{});
|
||||
BOOST_TEST_EQ(fn1(f1), 0);
|
||||
|
||||
compat::function_ref<int(F1&, int) const> fn2(compat::nontype_t<&F1::m2>{});
|
||||
BOOST_TEST_EQ(fn2(f1, 2), 12);
|
||||
|
||||
compat::function_ref<int(F1&, int, int) const> fn3(compat::nontype_t<&F1::m3>{});
|
||||
BOOST_TEST_EQ(fn3(f1, 2, 3), 123);
|
||||
|
||||
compat::function_ref<int(F1&, int, int, int) const> fn4(compat::nontype_t<&F1::m4>{});
|
||||
BOOST_TEST_EQ(fn4(f1, 2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<int(F1 const&)>, compat::nontype_t<&F1::m1>>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(F1 const&, int)>, compat::nontype_t<&F1::m2>>));
|
||||
|
||||
F1 f1;
|
||||
|
||||
compat::function_ref<int(F1 const&, int, int) const> fn3(compat::nontype_t<&F1::m3>{});
|
||||
BOOST_TEST_EQ(fn3(f1, 2, 3), 123);
|
||||
|
||||
compat::function_ref<int(F1 const&, int, int, int) const> fn4(compat::nontype_t<&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(compat::nontype_t<&F1::m3>{});
|
||||
BOOST_TEST_EQ(fn3(f2, 2, 3), 123);
|
||||
|
||||
compat::function_ref<int(F1 const&, int, int, int) const> fn24(compat::nontype_t<&F1::m4>{});
|
||||
BOOST_TEST_EQ(fn4(f2, 2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<int(int)>, compat::nontype_t<&F1::m2>, F1&>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(int)>, compat::nontype_t<&F1::m2>, F1 const&>));
|
||||
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<int(int)>, compat::nontype_t<&F1::m2>, F1&&>));
|
||||
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<int(int)>, compat::nontype_t<&F1::m2>, F1>));
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE(
|
||||
(std::is_constructible<compat::function_ref<int(int, int) const>, compat::nontype_t<&F1::m3>, F1&>));
|
||||
BOOST_TEST_TRAIT_TRUE(
|
||||
(std::is_constructible<compat::function_ref<int(int, int) const>, compat::nontype_t<&F1::m3>, F1 const&>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(int, int) const>, compat::nontype_t<&F1::m3>, F1&&>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(int, int) const>, compat::nontype_t<&F1::m3>, F1>));
|
||||
}
|
||||
|
||||
{
|
||||
F1 f1;
|
||||
|
||||
compat::function_ref<int()> fn1(compat::nontype_t<&F1::m1>{}, f1);
|
||||
BOOST_TEST_EQ(fn1(), 0);
|
||||
|
||||
compat::function_ref<int(int)> fn2(compat::nontype_t<&F1::m2>{}, f1);
|
||||
BOOST_TEST_EQ(fn2(2), 12);
|
||||
|
||||
compat::function_ref<int(int, int)> fn3(compat::nontype_t<&F1::m3>{}, f1);
|
||||
BOOST_TEST_EQ(fn3(2, 3), 123);
|
||||
|
||||
compat::function_ref<int(int, int, int)> fn4(compat::nontype_t<&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>, compat::nontype_t<&F1::m1>, F1&>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(int) const>, compat::nontype_t<&F1::m2>, F1&>));
|
||||
|
||||
compat::function_ref<int(int, int) const> fn3(compat::nontype_t<&F1::m3>{}, f1);
|
||||
BOOST_TEST_EQ(fn3(2, 3), 123);
|
||||
|
||||
compat::function_ref<int(int, int, int) const> fn4(compat::nontype_t<&F1::m4>{}, f1);
|
||||
BOOST_TEST_EQ(fn4(2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
{
|
||||
F1 f1;
|
||||
|
||||
compat::function_ref<int()> fn1(compat::nontype_t<&F1::m1>{}, &f1);
|
||||
BOOST_TEST_EQ(fn1(), 0);
|
||||
|
||||
compat::function_ref<int(int)> fn2(compat::nontype_t<&F1::m2>{}, &f1);
|
||||
BOOST_TEST_EQ(fn2(2), 12);
|
||||
|
||||
compat::function_ref<int(int, int)> fn3(compat::nontype_t<&F1::m3>{}, &f1);
|
||||
BOOST_TEST_EQ(fn3(2, 3), 123);
|
||||
|
||||
compat::function_ref<int(int, int, int)> fn4(compat::nontype_t<&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>, compat::nontype_t<&F1::m1>, F1 const*>));
|
||||
BOOST_TEST_TRAIT_FALSE(
|
||||
(std::is_constructible<compat::function_ref<int(int) const>, compat::nontype_t<&F1::m2>, F1 const*>));
|
||||
|
||||
compat::function_ref<int(int, int) const> fn3(compat::nontype_t<&F1::m3>{}, &f1);
|
||||
BOOST_TEST_EQ(fn3(2, 3), 123);
|
||||
|
||||
compat::function_ref<int(int, int, int) const> fn4(compat::nontype_t<&F1::m4>{}, &f1);
|
||||
BOOST_TEST_EQ(fn4(2, 3, 4), 1234);
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user