[libcxx-commits] [libcxx] [libc++] Implement `bind_back` (PR #81055)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Thu Feb 8 03:32:37 PST 2024


================
@@ -0,0 +1,356 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++03, c++11, c++14, c++17, c++20
+
+// <functional>
+
+// template<class F, class... Args>
+//   constexpr unspecified bind_back(F&& f, Args&&... args);
+
+#include <functional>
+
+#include <cassert>
+#include <tuple>
+#include <utility>
+
+#include "callable_types.h"
+#include "types.h"
+
+template <class Fn, class... Args>
+concept back_bindable =
+    requires(Fn&& fn, Args&&... args) { std::bind_back(std::forward<Fn>(fn), std::forward<Args>(args)...); };
+
+constexpr bool test() {
+  // Bind arguments, call without arguments
+  {
+    {
+      auto f = std::bind_back(MakeTuple{});
+      assert(f() == std::make_tuple());
+    }
+    {
+      auto f = std::bind_back(MakeTuple{}, Elem<1>{});
+      assert(f() == std::make_tuple(Elem<1>{}));
+    }
+    {
+      auto f = std::bind_back(MakeTuple{}, Elem<1>{}, Elem<2>{});
+      assert(f() == std::make_tuple(Elem<1>{}, Elem<2>{}));
+    }
+    {
+      auto f = std::bind_back(MakeTuple{}, Elem<1>{}, Elem<2>{}, Elem<3>{});
+      assert(f() == std::make_tuple(Elem<1>{}, Elem<2>{}, Elem<3>{}));
+    }
+  }
+
+  // Bind no arguments, call with arguments
+  {
+    {
+      auto f = std::bind_back(MakeTuple{});
+      assert(f(Elem<1>{}) == std::make_tuple(Elem<1>{}));
+    }
+    {
+      auto f = std::bind_back(MakeTuple{});
+      assert(f(Elem<1>{}, Elem<2>{}) == std::make_tuple(Elem<1>{}, Elem<2>{}));
+    }
+    {
+      auto f = std::bind_back(MakeTuple{});
+      assert(f(Elem<1>{}, Elem<2>{}, Elem<3>{}) == std::make_tuple(Elem<1>{}, Elem<2>{}, Elem<3>{}));
+    }
+  }
+
+  // Bind arguments, call with arguments
+  {
+    {
+      auto f = std::bind_back(MakeTuple{}, Elem<1>{});
+      assert(f(Elem<10>{}) == std::make_tuple(Elem<10>{}, Elem<1>{}));
+    }
+    {
+      auto f = std::bind_back(MakeTuple{}, Elem<1>{}, Elem<2>{});
+      assert(f(Elem<10>{}) == std::make_tuple(Elem<10>{}, Elem<1>{}, Elem<2>{}));
+    }
+    {
+      auto f = std::bind_back(MakeTuple{}, Elem<1>{}, Elem<2>{}, Elem<3>{});
+      assert(f(Elem<10>{}) == std::make_tuple(Elem<10>{}, Elem<1>{}, Elem<2>{}, Elem<3>{}));
+    }
+
+    {
+      auto f = std::bind_back(MakeTuple{}, Elem<1>{});
+      assert(f(Elem<10>{}, Elem<11>{}) == std::make_tuple(Elem<10>{}, Elem<11>{}, Elem<1>{}));
+    }
+    {
+      auto f = std::bind_back(MakeTuple{}, Elem<1>{}, Elem<2>{});
+      assert(f(Elem<10>{}, Elem<11>{}) == std::make_tuple(Elem<10>{}, Elem<11>{}, Elem<1>{}, Elem<2>{}));
+    }
+    {
+      auto f = std::bind_back(MakeTuple{}, Elem<1>{}, Elem<2>{}, Elem<3>{});
+      assert(f(Elem<10>{}, Elem<11>{}) == std::make_tuple(Elem<10>{}, Elem<11>{}, Elem<1>{}, Elem<2>{}, Elem<3>{}));
+    }
+  }
+
+  // Basic tests with fundamental types
+  {
+    int n     = 2;
+    int m     = 1;
+    auto add  = [](int x, int y) { return x + y; };
+    auto addN = [](int a, int b, int c, int d, int e, int f) { return a + b + c + d + e + f; };
+
+    auto a = std::bind_back(add, m, n);
+    assert(a() == 3);
+
+    auto b = std::bind_back(addN, m, n, m, m, m, m);
+    assert(b() == 7);
+
+    auto c = std::bind_back(addN, n, m);
+    assert(c(1, 1, 1, 1) == 7);
+
+    auto f = std::bind_back(add, n);
+    assert(f(3) == 5);
+
+    auto g = std::bind_back(add, n, 1);
+    assert(g() == 3);
+
+    auto h = std::bind_back(addN, 1, 1, 1);
+    assert(h(2, 2, 2) == 9);
+  }
+
+  // Make sure we don't treat std::reference_wrapper specially.
+  {
+    auto sub = [](std::reference_wrapper<int> a, std::reference_wrapper<int> b) { return a.get() - b.get(); };
+    int i = 1, j = 2;
+    auto f = std::bind_back(sub, std::ref(i));
+    assert(f(std::ref(j)) == 2 - 1);
+  }
+
+  // Make sure we can call a function that's a pointer to a member function.
+  {
+    struct MemberFunction {
+      constexpr bool foo(int, int) { return true; }
+    };
+    MemberFunction value;
+    auto fn = std::bind_back(&MemberFunction::foo, 0, 0);
+    assert(fn(value));
+  }
+
+  // Make sure that we copy the bound arguments into the unspecified-type.
+  {
+    auto add = [](int x, int y) { return x + y; };
+    int n    = 2;
+    auto i   = std::bind_back(add, n, 1);
+    n        = 100;
+    assert(i() == 3);
+  }
+
+  // Make sure we pass the bound arguments to the function object
+  // with the right value category.
+  {
+    {
+      auto wasCopied = [](CopyMoveInfo info) { return info.copy_kind == CopyMoveInfo::copy; };
+      CopyMoveInfo info;
+      auto copied = std::bind_back(wasCopied, info);
+      assert(copied());
+    }
+
+    {
+      auto wasMoved = [](CopyMoveInfo info) { return info.copy_kind == CopyMoveInfo::move; };
+      CopyMoveInfo info;
+      auto moved = std::bind_back(wasMoved, info);
+      assert(std::move(moved)());
+    }
+  }
+
+  // Make sure we call the correctly cv-ref qualified operator() based on the
+  // value category of the bind_back unspecified-type.
+  {
+    struct F {
+      constexpr int operator()() & { return 1; }
+      constexpr int operator()() const& { return 2; }
+      constexpr int operator()() && { return 3; }
+      constexpr int operator()() const&& { return 4; }
+    };
+    auto x  = std::bind_back(F{});
+    using X = decltype(x);
+    assert(static_cast<X&>(x)() == 1);
+    assert(static_cast<X const&>(x)() == 2);
+    assert(static_cast<X&&>(x)() == 3);
+    assert(static_cast<X const&&>(x)() == 4);
+  }
+
+  // Make sure the bind_back unspecified-type is NOT invocable when the call would select a
+  // differently-qualified operator().
+  //
+  // For example, if the call to `operator()() &` is ill-formed, the call to the unspecified-type
+  // should be ill-formed and not fall back to the `operator()() const&` overload.
+  { // Make sure we delete the & overload when the underlying call isn't valid
+    {
+      struct F {
+        void operator()() & = delete;
+        void operator()() const&;
+        void operator()() &&;
+        void operator()() const&&;
+      };
+      using X = decltype(std::bind_back(F{}));
+      static_assert(!std::is_invocable_v<X&>);
+      static_assert(std::is_invocable_v<X const&>);
+      static_assert(std::is_invocable_v<X>);
+      static_assert(std::is_invocable_v<X const>);
+    }
+
+    // There's no way to make sure we delete the const& overload when the underlying call isn't valid,
+    // so we can't check this one.
+
+    // Make sure we delete the && overload when the underlying call isn't valid
+    {
+      struct F {
+        void operator()() &;
+        void operator()() const&;
+        void operator()() && = delete;
+        void operator()() const&&;
+      };
+      using X = decltype(std::bind_back(F{}));
+      static_assert(std::is_invocable_v<X&>);
+      static_assert(std::is_invocable_v<X const&>);
+      static_assert(!std::is_invocable_v<X>);
+      static_assert(std::is_invocable_v<X const>);
+    }
+
+    // Make sure we delete the const&& overload when the underlying call isn't valid
+    {
+      struct F {
+        void operator()() &;
+        void operator()() const&;
+        void operator()() &&;
+        void operator()() const&& = delete;
+      };
+      using X = decltype(std::bind_back(F{}));
+      static_assert(std::is_invocable_v<X&>);
+      static_assert(std::is_invocable_v<X const&>);
+      static_assert(std::is_invocable_v<X>);
+      static_assert(!std::is_invocable_v<X const>);
+    }
+  }
+
+  // Some examples by Tim Song
+  {
+    {
+      struct T {};
+      struct F {
+        void operator()(T&&) const&;
+        void operator()(T&&) && = delete;
+      };
+      using X = decltype(std::bind_back(F{}));
+      static_assert(!std::is_invocable_v<X, T>);
+    }
+
+    {
+      struct T {};
+      struct F {
+        void operator()(T const&) const;
+        void operator()(T&&) const = delete;
+      };
+      using X = decltype(std::bind_back(F{}, T{}));
+      static_assert(!std::is_invocable_v<X>);
+    }
+  }
+
+  // Test properties of the constructor of the unspecified-type returned by bind_back.
+  {
+    {
+      MoveOnlyCallable<bool> value(true);
+      auto ret = std::bind_back(std::move(value), 1);
+      assert(ret());
+      assert(ret(1, 2, 3));
+
+      auto ret1 = std::move(ret);
+      assert(!ret());
+      assert(ret1());
+      assert(ret1(1, 2, 3));
+
+      using RetT = decltype(ret);
+      static_assert(std::is_move_constructible<RetT>::value);
+      static_assert(!std::is_copy_constructible<RetT>::value);
+      static_assert(!std::is_move_assignable<RetT>::value);
+      static_assert(!std::is_copy_assignable<RetT>::value);
+    }
+    {
+      CopyCallable<bool> value(true);
+      auto ret = std::bind_back(value, 1);
+      assert(ret());
+      assert(ret(1, 2, 3));
+
+      auto ret1 = std::move(ret);
+      assert(ret1());
+      assert(ret1(1, 2, 3));
+
+      auto ret2 = std::bind_back(std::move(value), 1);
+      assert(!ret());
+      assert(ret2());
+      assert(ret2(1, 2, 3));
+
+      using RetT = decltype(ret);
+      static_assert(std::is_move_constructible<RetT>::value);
+      static_assert(std::is_copy_constructible<RetT>::value);
+      static_assert(!std::is_move_assignable<RetT>::value);
+      static_assert(!std::is_copy_assignable<RetT>::value);
+    }
+    {
+      CopyAssignableWrapper value(true);
+      using RetT = decltype(std::bind_back(value, 1));
+
+      static_assert(std::is_move_constructible<RetT>::value);
+      static_assert(std::is_copy_constructible<RetT>::value);
+      static_assert(std::is_move_assignable<RetT>::value);
+      static_assert(std::is_copy_assignable<RetT>::value);
+    }
+    {
+      MoveAssignableWrapper value(true);
+      using RetT = decltype(std::bind_back(std::move(value), 1));
+
+      static_assert(std::is_move_constructible<RetT>::value);
+      static_assert(!std::is_copy_constructible<RetT>::value);
+      static_assert(std::is_move_assignable<RetT>::value);
+      static_assert(!std::is_copy_assignable<RetT>::value);
+    }
+  }
+
+  // Make sure bind_back is SFINAE friendly
+  {
+    static_assert(!std::is_constructible_v<NotCopyMove, NotCopyMove&>);
+    static_assert(!std::is_move_constructible_v<NotCopyMove>);
+    static_assert(!back_bindable<NotCopyMove>);
+    static_assert(!back_bindable<NotCopyMove&>);
----------------
frederick-vs-ja wrote:

Perhaps we should use `LIBCPP_STATIC_ASSERT` for libc++-specific SFINAE-friendliness, since the standard uses _Mandates_ for unsuitable parameter types ([[func.bind.partial]/2](https://eel.is/c++draft/func.bind.partial#2)).

Ditto for other occurences of `back_bindable` and pre-existing test cases for `bind_front`.

```suggestion
    LIBCPP_STATIC_ASSERT(!back_bindable<NotCopyMove>);
    LIBCPP_STATIC_ASSERT(!back_bindable<NotCopyMove&>);
```

https://github.com/llvm/llvm-project/pull/81055


More information about the libcxx-commits mailing list