[libcxx-commits] [libcxx] [libc++] Implement std::move_only_function (P0288R9) (PR #94670)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Nov 21 08:21:02 PST 2025
================
@@ -0,0 +1,62 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+#include <cassert>
+#include <concepts>
+#include <functional>
+#include <utility>
+
+#include "type_algorithms.h"
+#include "../common.h"
+
+template <class T>
+void test() {
+ {
+ std::move_only_function<void() const noexcept> f1;
+ std::move_only_function<T> f2;
+ std::same_as<std::move_only_function<T>&> decltype(auto) ret = (f2 = std::move(f1));
+ assert(&ret == &f2);
+ assert(!f2);
+ }
+ {
+ std::move_only_function<void() const & noexcept> f1;
+ std::move_only_function<T> f2;
+ std::same_as<std::move_only_function<T>&> decltype(auto) ret = (f2 = std::move(f1));
+ assert(&ret == &f2);
+ assert(!f2);
+ }
+}
+
+template <class T>
+void test2() {
+ {
+ std::move_only_function<int() const noexcept> f1 = [] noexcept { return 109; };
+ std::move_only_function<T> f2;
+ std::same_as<std::move_only_function<T>&> decltype(auto) ret = (f2 = std::move(f1));
+ assert(&ret == &f2);
+ assert(f2);
+ assert(f2() == 109);
+ }
+ {
+ std::move_only_function<int() const& noexcept> f1 = [] noexcept { return 109; };
+ std::move_only_function<T> f2;
+ std::same_as<std::move_only_function<T>&> decltype(auto) ret = (f2 = std::move(f1));
+ assert(&ret == &f2);
+ assert(f2);
+ assert(f2() == 109);
+ }
+}
+
+int main(int, char**) {
+ types::for_each(types::function_noexcept_const_ref_qualified<void()>{}, []<class T> { test<T>(); });
+ types::for_each(types::function_noexcept_const_lvalue_ref_qualified<int()>{}, []<class T> { test2<T>(); });
----------------
ldionne wrote:
Can you explain why this one place is different from everywhere else and uses `function_noexcept_const_lvalue_ref_qualified` in addition to `function_noexcept_const_ref_qualified`? Leaving a comment behind is probably good.
https://github.com/llvm/llvm-project/pull/94670
More information about the libcxx-commits
mailing list