@@ -154,7 +154,9 @@ bits_headers = \
${bits_srcdir}/mask_array.h \
${bits_srcdir}/max_size_type.h \
${bits_srcdir}/memoryfwd.h \
+ ${bits_srcdir}/mofunc_impl.h \
${bits_srcdir}/move.h \
+ ${bits_srcdir}/move_only_function.h \
${bits_srcdir}/node_handle.h \
${bits_srcdir}/ostream.tcc \
${bits_srcdir}/ostream_insert.h \
new file mode 100644
@@ -0,0 +1,200 @@
+// Implementation of std::move_only_function -*- C++ -*-
+
+// Copyright The GNU Toolchain Authors.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file include/bits/mofunc_impl.h
+ * This is an internal header file, included by other library headers.
+ * Do not attempt to use it directly. @headername{functional}
+ */
+
+#ifndef _GLIBCXX_MOF_CV
+# define _GLIBCXX_MOF_CV
+#endif
+
+#ifdef _GLIBCXX_MOF_REF
+# define _GLIBCXX_MOF_INV_QUALS _GLIBCXX_MOF_CV _GLIBCXX_MOF_REF
+#else
+# define _GLIBCXX_MOF_REF
+# define _GLIBCXX_MOF_INV_QUALS _GLIBCXX_MOF_CV &
+#endif
+
+#define _GLIBCXX_MOF_CV_REF _GLIBCXX_MOF_CV _GLIBCXX_MOF_REF
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+ template<typename _Res, typename... _ArgTypes, bool _Noex>
+ class move_only_function<_Res(_ArgTypes...) _GLIBCXX_MOF_CV
+ _GLIBCXX_MOF_REF noexcept(_Noex)>
+ : _Mofunc_base
+ {
+ template<typename _Tp>
+ using __callable
+ = __conditional_t<_Noex,
+ is_nothrow_invocable_r<_Res, _Tp, _ArgTypes...>,
+ is_invocable_r<_Res, _Tp, _ArgTypes...>>;
+
+ // [func.wrap.mov.con]/1 is-callable-from<VT>
+ template<typename _Vt>
+ static constexpr bool __is_callable_from
+ = __and_v<__callable<_Vt _GLIBCXX_MOF_CV_REF>,
+ __callable<_Vt _GLIBCXX_MOF_INV_QUALS>>;
+
+ public:
+ using result_type = _Res;
+
+ move_only_function() noexcept { }
+
+ move_only_function(nullptr_t) noexcept { }
+
+ move_only_function(move_only_function&& __x) noexcept
+ : _Mofunc_base(static_cast<_Mofunc_base&&>(__x)),
+ _M_invoke(std::__exchange(__x._M_invoke, nullptr))
+ { }
+
+ template<typename _Fn, typename _Vt = decay_t<_Fn>>
+ requires (!is_same_v<_Vt, move_only_function>)
+ && (!__is_in_place_type_v<_Vt>) && __is_callable_from<_Vt>
+ move_only_function(_Fn&& __f) noexcept(_S_nothrow_init<_Vt, _Fn>())
+ {
+ if constexpr (is_function_v<remove_pointer_t<_Vt>>
+ || is_member_pointer_v<_Vt>
+ || __is_move_only_function_v<_Vt>)
+ {
+ if (__f == nullptr)
+ return;
+ }
+ _M_init<_Vt>(std::forward<_Fn>(__f));
+ _M_invoke = &_S_invoke<_Vt>;
+ }
+
+ template<typename _Tp, typename... _Args>
+ requires is_constructible_v<_Tp, _Args...>
+ && __is_callable_from<_Tp>
+ explicit
+ move_only_function(in_place_type_t<_Tp>, _Args&&... __args)
+ noexcept(_S_nothrow_init<_Tp, _Args...>())
+ : _M_invoke(&_S_invoke<_Tp>)
+ {
+ static_assert(is_same_v<decay_t<_Tp>, _Tp>);
+ _M_init<_Tp>(std::forward<_Args>(__args)...);
+ }
+
+ template<typename _Tp, typename _Up, typename... _Args>
+ requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
+ && __is_callable_from<_Tp>
+ explicit
+ move_only_function(in_place_type_t<_Tp>, initializer_list<_Up> __il,
+ _Args&&... __args)
+ noexcept(_S_nothrow_init<_Tp, initializer_list<_Up>&, _Args...>())
+ : _Mofunc_base(nullptr), _M_invoke(&_S_invoke<_Tp>)
+ {
+ static_assert(is_same_v<decay_t<_Tp>, _Tp>);
+ _M_init<_Tp>(__il, std::forward<_Args>(__args)...);
+ }
+
+ move_only_function&
+ operator=(move_only_function&& __x) noexcept
+ {
+ _Mofunc_base::operator=(static_cast<_Mofunc_base&&>(__x));
+ _M_invoke = std::__exchange(__x._M_invoke, nullptr);
+ return *this;
+ }
+
+ move_only_function&
+ operator=(nullptr_t) noexcept
+ {
+ _Mofunc_base::operator=(nullptr);
+ _M_invoke = nullptr;
+ return *this;
+ }
+
+ template<typename _Fn>
+ requires is_constructible_v<move_only_function, _Fn>
+ move_only_function&
+ operator=(_Fn&& __f)
+ noexcept(is_nothrow_constructible_v<move_only_function, _Fn>)
+ {
+ move_only_function(std::forward<_Fn>(__f)).swap(*this);
+ return *this;
+ }
+
+ ~move_only_function() = default;
+
+ explicit operator bool() const noexcept { return _M_invoke != nullptr; }
+
+ _Res
+ operator()(_ArgTypes... __args) _GLIBCXX_MOF_CV_REF noexcept(_Noex)
+ {
+ __glibcxx_assert(*this != nullptr);
+ return _M_invoke(this, std::forward<_ArgTypes>(__args)...);
+ }
+
+ void
+ swap(move_only_function& __x) noexcept
+ {
+ _Mofunc_base::swap(__x);
+ std::swap(_M_invoke, __x._M_invoke);
+ }
+
+ friend void
+ swap(move_only_function& __x, move_only_function& __y) noexcept
+ { __x.swap(__y); }
+
+ friend bool
+ operator==(const move_only_function& __x, nullptr_t) noexcept
+ { return __x._M_invoke == nullptr; }
+
+ private:
+ template<typename _Tp>
+ using __param_t
+ = __conditional_t<is_trivially_copyable_v<_Tp>
+ && sizeof(_Tp) <= sizeof(long),
+ _Tp, _Tp&&>;
+
+ using _Invoker = _Res (*)(_Mofunc_base _GLIBCXX_MOF_CV*,
+ __param_t<_ArgTypes>...) noexcept(_Noex);
+
+ template<typename _Tp>
+ static _Res
+ _S_invoke(_Mofunc_base _GLIBCXX_MOF_CV* __self,
+ __param_t<_ArgTypes>... __args) noexcept(_Noex)
+ {
+ using _TpCv = _Tp _GLIBCXX_MOF_CV;
+ using _TpInv = _Tp _GLIBCXX_MOF_INV_QUALS;
+ return std::__invoke_r<_Res>(
+ std::forward<_TpInv>(*_S_access<_TpCv>(__self)),
+ std::forward<__param_t<_ArgTypes>>(__args)...);
+ }
+
+ _Invoker _M_invoke = nullptr;
+ };
+
+#undef _GLIBCXX_MOF_CV_REF
+#undef _GLIBCXX_MOF_CV
+#undef _GLIBCXX_MOF_REF
+#undef _GLIBCXX_MOF_INV_QUALS
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace std
new file mode 100644
@@ -0,0 +1,204 @@
+// Implementation of std::move_only_function -*- C++ -*-
+
+// Copyright The GNU Toolchain Authors.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file include/bits/move_only_function.h
+ * This is an internal header file, included by other library headers.
+ * Do not attempt to use it directly. @headername{functional}
+ */
+
+#ifndef _GLIBCXX_MOVE_ONLY_FUNCTION_H
+#define _GLIBCXX_MOVE_ONLY_FUNCTION_H 1
+
+#pragma GCC system_header
+
+#if __cplusplus > 202002L
+
+#include <bits/invoke.h>
+#include <bits/utility.h>
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+#define __cpp_lib_move_only_function 202110L
+
+ template<typename... _Signature>
+ class move_only_function; // not defined
+
+ /// @cond undocumented
+ class _Mofunc_base
+ {
+ protected:
+ _Mofunc_base() noexcept
+ : _M_manage(_S_empty)
+ { }
+
+ _Mofunc_base(_Mofunc_base&& __x) noexcept
+ {
+ _M_manage = std::__exchange(__x._M_manage, _S_empty);
+ _M_manage(_M_storage, &__x._M_storage);
+ }
+
+ template<typename _Tp, typename... _Args>
+ static constexpr bool
+ _S_nothrow_init() noexcept
+ {
+ if constexpr (__stored_locally<_Tp>)
+ return is_nothrow_constructible_v<_Tp, _Args...>;
+ return false;
+ }
+
+ template<typename _Tp, typename... _Args>
+ void
+ _M_init(_Args&&... __args) noexcept(_S_nothrow_init<_Tp, _Args...>())
+ {
+ if constexpr (__stored_locally<_Tp>)
+ ::new (_M_storage._M_addr()) _Tp(std::forward<_Args>(__args)...);
+ else
+ _M_storage._M_p = new _Tp(std::forward<_Args>(__args)...);
+
+ _M_manage = &_S_manage<_Tp>;
+ }
+
+ _Mofunc_base&
+ operator=(_Mofunc_base&& __x) noexcept
+ {
+ _M_manage(_M_storage, nullptr);
+ _M_manage = std::__exchange(__x._M_manage, _S_empty);
+ _M_manage(_M_storage, &__x._M_storage);
+ return *this;
+ }
+
+ _Mofunc_base&
+ operator=(nullptr_t) noexcept
+ {
+ _M_manage(_M_storage, nullptr);
+ _M_manage = _S_empty;
+ return *this;
+ }
+
+ ~_Mofunc_base() { _M_manage(_M_storage, nullptr); }
+
+ void
+ swap(_Mofunc_base& __x) noexcept
+ {
+ // Order of operations here is more efficient if __x is empty.
+ _Storage __s;
+ __x._M_manage(__s, &__x._M_storage);
+ _M_manage(__x._M_storage, &_M_storage);
+ __x._M_manage(_M_storage, &__s);
+ std::swap(_M_manage, __x._M_manage);
+ }
+
+ template<typename _Tp, typename _Self>
+ static _Tp*
+ _S_access(_Self* __self) noexcept
+ {
+ if constexpr (__stored_locally<remove_const_t<_Tp>>)
+ return static_cast<_Tp*>(__self->_M_storage._M_addr());
+ else
+ return static_cast<_Tp*>(__self->_M_storage._M_p);
+ }
+
+ private:
+ struct _Storage
+ {
+ void* _M_addr() noexcept { return &_M_bytes[0]; }
+ const void* _M_addr() const noexcept { return &_M_bytes[0]; }
+
+ // We want to have enough space to store a simple delegate type.
+ struct _Delegate { void (_Storage::*__pfm)(); _Storage* __obj; };
+ union {
+ void* _M_p;
+ alignas(_Delegate) alignas(void(*)())
+ unsigned char _M_bytes[sizeof(_Delegate)];
+ };
+ };
+
+ template<typename _Tp>
+ static constexpr bool __stored_locally
+ = sizeof(_Tp) <= sizeof(_Storage) && alignof(_Tp) <= alignof(_Storage)
+ && is_nothrow_move_constructible_v<_Tp>;
+
+ // A function that either destroys the target object stored in __target,
+ // or moves the target object from *__src to __target.
+ using _Manager = void (*)(_Storage& __target, _Storage* __src) noexcept;
+
+ // The no-op manager function for objects with no target.
+ static void _S_empty(_Storage&, _Storage*) noexcept { }
+
+ // The real manager function for a target object of type _Tp.
+ template<typename _Tp>
+ static void
+ _S_manage(_Storage& __target, _Storage* __src) noexcept
+ {
+ if constexpr (__stored_locally<_Tp>)
+ {
+ if (__src)
+ {
+ _Tp* __rval = static_cast<_Tp*>(__src->_M_addr());
+ ::new (__target._M_addr()) _Tp(std::move(*__rval));
+ __rval->~_Tp();
+ }
+ else
+ static_cast<_Tp*>(__target._M_addr())->~_Tp();
+ }
+ else
+ {
+ if (__src)
+ __target._M_p = __src->_M_p;
+ else
+ delete static_cast<_Tp*>(__target._M_p);
+ }
+ }
+
+ _Storage _M_storage;
+ _Manager _M_manage;
+ };
+
+ template<typename _Tp>
+ inline constexpr bool __is_move_only_function_v = false;
+ template<typename _Tp>
+ constexpr bool __is_move_only_function_v<move_only_function<_Tp>> = true;
+ /// @endcond
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace std
+
+#include "mofunc_impl.h"
+#define _GLIBCXX_MOF_CV const
+#include "mofunc_impl.h"
+#define _GLIBCXX_MOF_REF &
+#include "mofunc_impl.h"
+#define _GLIBCXX_MOF_REF &&
+#include "mofunc_impl.h"
+#define _GLIBCXX_MOF_CV const
+#define _GLIBCXX_MOF_REF &
+#include "mofunc_impl.h"
+#define _GLIBCXX_MOF_CV const
+#define _GLIBCXX_MOF_REF &&
+#include "mofunc_impl.h"
+
+#endif // C++23
+#endif // _GLIBCXX_MOVE_ONLY_FUNCTION_H
@@ -67,6 +67,9 @@
# include <bits/ranges_cmp.h>
# include <compare>
#endif
+#if __cplusplus > 202002L
+# include <bits/move_only_function.h>
+#endif
#endif // C++11
@@ -284,6 +284,7 @@
#define __cpp_lib_adaptor_iterator_pair_constructor 202106L
#define __cpp_lib_invoke_r 202106L
#define __cpp_lib_is_scoped_enum 202011L
+#define __cpp_lib_move_only_function 202110L
#define __cpp_lib_string_contains 202011L
#define __cpp_lib_to_underlying 202102L
#endif // C++2b
new file mode 100644
@@ -0,0 +1,199 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do run { target c++23 } }
+
+#include <functional>
+#include <utility>
+#include <testsuite_hooks.h>
+
+using std::move_only_function;
+
+using std::is_same_v;
+using std::is_invocable_v;
+using std::is_nothrow_invocable_v;
+using std::invoke_result_t;
+
+// Check return types
+static_assert( is_same_v<void, invoke_result_t<move_only_function<void()>>> );
+static_assert( is_same_v<int, invoke_result_t<move_only_function<int()>>> );
+static_assert( is_same_v<int&, invoke_result_t<move_only_function<int&()>>> );
+
+// With const qualifier
+static_assert( ! is_invocable_v< move_only_function<void()> const > );
+static_assert( ! is_invocable_v< move_only_function<void()> const &> );
+static_assert( is_invocable_v< move_only_function<void() const> > );
+static_assert( is_invocable_v< move_only_function<void() const> &> );
+static_assert( is_invocable_v< move_only_function<void() const> const > );
+static_assert( is_invocable_v< move_only_function<void() const> const &> );
+
+// With no ref-qualifier
+static_assert( is_invocable_v< move_only_function<void()> > );
+static_assert( is_invocable_v< move_only_function<void()> &> );
+static_assert( is_invocable_v< move_only_function<void() const> > );
+static_assert( is_invocable_v< move_only_function<void() const> &> );
+static_assert( is_invocable_v< move_only_function<void() const> const > );
+static_assert( is_invocable_v< move_only_function<void() const> const &> );
+
+// With & ref-qualifier
+static_assert( ! is_invocable_v< move_only_function<void()&> > );
+static_assert( is_invocable_v< move_only_function<void()&> &> );
+static_assert( is_invocable_v< move_only_function<void() const&> > );
+static_assert( is_invocable_v< move_only_function<void() const&> &> );
+static_assert( is_invocable_v< move_only_function<void() const&> const > );
+static_assert( is_invocable_v< move_only_function<void() const&> const &> );
+
+// With && ref-qualifier
+static_assert( is_invocable_v< move_only_function<void()&&> > );
+static_assert( ! is_invocable_v< move_only_function<void()&&> &> );
+static_assert( is_invocable_v< move_only_function<void() const&&> > );
+static_assert( ! is_invocable_v< move_only_function<void() const&&> &> );
+static_assert( is_invocable_v< move_only_function<void() const&&> const > );
+static_assert( ! is_invocable_v< move_only_function<void() const&&> const &> );
+
+// With noexcept-specifier
+static_assert( ! is_nothrow_invocable_v< move_only_function<void()> > );
+static_assert( ! is_nothrow_invocable_v< move_only_function<void() noexcept(false)> > );
+static_assert( is_nothrow_invocable_v< move_only_function<void() noexcept> > );
+static_assert( is_nothrow_invocable_v< move_only_function<void()& noexcept>& > );
+
+void
+test01()
+{
+ struct F
+ {
+ int operator()() { return 0; }
+ int operator()() const { return 1; }
+ };
+
+ move_only_function<int()> f0{F{}};
+ VERIFY( f0() == 0 );
+ VERIFY( std::move(f0)() == 0 );
+
+ move_only_function<int() const> f1{F{}};
+ VERIFY( f1() == 1 );
+ VERIFY( std::as_const(f1)() == 1 );
+ VERIFY( std::move(f1)() == 1 );
+ VERIFY( std::move(std::as_const(f1))() == 1 );
+
+ move_only_function<int()&> f2{F{}};
+ VERIFY( f2() == 0 );
+ // Not rvalue-callable: std::move(f2)()
+
+ move_only_function<int() const&> f3{F{}};
+ VERIFY( f3() == 1 );
+ VERIFY( std::as_const(f3)() == 1 );
+ VERIFY( std::move(f3)() == 1 );
+ VERIFY( std::move(std::as_const(f3))() == 1 );
+
+ move_only_function<int()&&> f4{F{}};
+ // Not lvalue-callable: f4()
+ VERIFY( std::move(f4)() == 0 );
+
+ move_only_function<int() const&&> f5{F{}};
+ // Not lvalue-callable: f5()
+ VERIFY( std::move(f5)() == 1 );
+ VERIFY( std::move(std::as_const(f5))() == 1 );
+}
+
+void
+test02()
+{
+ struct F
+ {
+ int operator()() & { return 0; }
+ int operator()() && { return 1; }
+ };
+
+ move_only_function<int()> f0{F{}};
+ VERIFY( f0() == 0 );
+ VERIFY( std::move(f0)() == 0 );
+
+ move_only_function<int()&&> f1{F{}};
+ // Not lvalue callable: f1()
+ VERIFY( std::move(f1)() == 1 );
+
+ move_only_function<int()&> f2{F{}};
+ VERIFY( f2() == 0 );
+ // Not rvalue-callable: std::move(f2)()
+}
+
+void
+test03()
+{
+ struct F
+ {
+ int operator()() const & { return 0; }
+ int operator()() && { return 1; }
+ };
+
+ move_only_function<int()> f0{F{}};
+ VERIFY( f0() == 0 );
+ VERIFY( std::move(f0)() == 0 );
+
+ move_only_function<int()&&> f1{F{}};
+ // Not lvalue callable: f1()
+ VERIFY( std::move(f1)() == 1 );
+
+ move_only_function<int() const> f2{F{}};
+ VERIFY( f2() == 0 );
+ VERIFY( std::as_const(f2)() == 0 );
+ VERIFY( std::move(f2)() == 0 );
+ VERIFY( std::move(std::as_const(f2))() == 0 );
+
+ move_only_function<int() const &&> f3{F{}};
+ // Not lvalue callable: f3()
+ VERIFY( std::move(f3)() == 0 );
+ VERIFY( std::move(std::as_const(f3))() == 0 );
+
+ move_only_function<int() const &> f4{F{}};
+ VERIFY( f4() == 0 );
+ VERIFY( std::as_const(f4)() == 0 );
+ // Not rvalue-callable: std::move(f4)()
+}
+
+void
+test04()
+{
+ struct F
+ {
+ int operator()() & { return 0; }
+ int operator()() && { return 1; }
+ int operator()() const & { return 2; }
+ int operator()() const && { return 3; }
+ };
+
+ move_only_function<int()> f0{F{}};
+ VERIFY( f0() == 0 );
+ VERIFY( std::move(f0)() == 0 );
+
+ move_only_function<int()&> f1{F{}};
+ VERIFY( f1() == 0 );
+ // Not rvalue-callable: std::move(f1)()
+
+ move_only_function<int()&&> f2{F{}};
+ // Not lvalue callable: f2()
+ VERIFY( std::move(f2)() == 1 );
+
+ move_only_function<int() const> f3{F{}};
+ VERIFY( f3() == 2 );
+ VERIFY( std::as_const(f3)() == 2 );
+ VERIFY( std::move(f3)() == 2 );
+ VERIFY( std::move(std::as_const(f3))() == 2 );
+
+ move_only_function<int() const &> f4{F{}};
+ VERIFY( f4() == 2 );
+ VERIFY( std::as_const(f4)() == 2 );
+ // Not rvalue-callable: std::move(f4)()
+
+ move_only_function<int() const &&> f5{F{}};
+ // Not lvalue callable: f5()
+ VERIFY( std::move(f5)() == 3 );
+ VERIFY( std::move(std::as_const(f5))() == 3 );
+}
+
+int main()
+{
+ test01();
+ test02();
+ test03();
+ test04();
+}
new file mode 100644
@@ -0,0 +1,98 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <functional>
+
+#ifndef __cpp_lib_move_only_function
+# error "Feature-test macro for move_only_function missing in <functional>"
+#elif __cpp_lib_move_only_function != 202110L
+# error "Feature-test macro for move_only_function has wrong value in <functional>"
+#endif
+
+using std::move_only_function;
+
+using std::is_constructible_v;
+using std::is_copy_constructible_v;
+using std::is_nothrow_default_constructible_v;
+using std::is_nothrow_move_constructible_v;
+using std::is_nothrow_constructible_v;
+using std::nullptr_t;
+using std::in_place_type_t;
+
+static_assert( is_nothrow_default_constructible_v<move_only_function<void()>> );
+static_assert( is_nothrow_constructible_v<move_only_function<void()>, nullptr_t> );
+static_assert( is_nothrow_move_constructible_v<move_only_function<void()>> );
+static_assert( ! is_copy_constructible_v<move_only_function<void()>> );
+
+static_assert( is_constructible_v<move_only_function<void()>, void()> );
+static_assert( is_constructible_v<move_only_function<void()>, void(&)()> );
+static_assert( is_constructible_v<move_only_function<void()>, void(*)()> );
+static_assert( is_constructible_v<move_only_function<void()>, int()> );
+static_assert( is_constructible_v<move_only_function<void()>, int(&)()> );
+static_assert( is_constructible_v<move_only_function<void()>, int(*)()> );
+static_assert( ! is_constructible_v<move_only_function<void()>, void(int)> );
+static_assert( is_constructible_v<move_only_function<void(int)>, void(int)> );
+
+static_assert( is_constructible_v<move_only_function<void(int)>,
+ in_place_type_t<void(*)(int)>, void(int)> );
+
+static_assert( is_constructible_v<move_only_function<void()>,
+ void() noexcept> );
+static_assert( is_constructible_v<move_only_function<void() noexcept>,
+ void() noexcept> );
+static_assert( ! is_constructible_v<move_only_function<void() noexcept>,
+ void() > );
+
+struct Q
+{
+ void operator()() const &;
+ void operator()() &&;
+};
+
+static_assert( is_constructible_v<move_only_function<void()>, Q> );
+static_assert( is_constructible_v<move_only_function<void() const>, Q> );
+static_assert( is_constructible_v<move_only_function<void() &>, Q> );
+static_assert( is_constructible_v<move_only_function<void() const &>, Q> );
+static_assert( is_constructible_v<move_only_function<void() &&>, Q> );
+static_assert( is_constructible_v<move_only_function<void() const &&>, Q> );
+
+struct R
+{
+ void operator()() &;
+ void operator()() &&;
+};
+
+static_assert( is_constructible_v<move_only_function<void()>, R> );
+static_assert( is_constructible_v<move_only_function<void()&>, R> );
+static_assert( is_constructible_v<move_only_function<void()&&>, R> );
+static_assert( ! is_constructible_v<move_only_function<void() const>, R> );
+static_assert( ! is_constructible_v<move_only_function<void() const&>, R> );
+static_assert( ! is_constructible_v<move_only_function<void() const&&>, R> );
+
+// The following nothrow-constructible guarantees are a GCC extension,
+// not required by the standard.
+
+static_assert( is_nothrow_constructible_v<move_only_function<void()>, void()> );
+static_assert( is_nothrow_constructible_v<move_only_function<void(int)>,
+ in_place_type_t<void(*)(int)>,
+ void(int)> );
+
+// These types are all small and nothrow move constructible
+struct F { void operator()(); };
+struct G { void operator()() const; };
+static_assert( is_nothrow_constructible_v<move_only_function<void()>, F> );
+static_assert( is_nothrow_constructible_v<move_only_function<void()>, G> );
+static_assert( is_nothrow_constructible_v<move_only_function<void() const>, G> );
+
+struct H {
+ H(int);
+ H(int, int) noexcept;
+ void operator()() noexcept;
+};
+static_assert( is_nothrow_constructible_v<move_only_function<void()>, H> );
+static_assert( is_nothrow_constructible_v<move_only_function<void() noexcept>,
+ H> );
+static_assert( ! is_nothrow_constructible_v<move_only_function<void() noexcept>,
+ in_place_type_t<H>, int> );
+static_assert( is_nothrow_constructible_v<move_only_function<void() noexcept>,
+ in_place_type_t<H>, int, int> );
new file mode 100644
@@ -0,0 +1,109 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do run { target c++23 } }
+
+#include <functional>
+#include <testsuite_hooks.h>
+
+using std::move_only_function;
+
+void
+test01()
+{
+ // Small type with non-throwing move constructor. Not allocated on the heap.
+ struct F
+ {
+ F() = default;
+ F(const F& f) : counters(f.counters) { ++counters.copy; }
+ F(F&& f) noexcept : counters(f.counters) { ++counters.move; }
+
+ F& operator=(F&&) = delete;
+
+ struct Counters
+ {
+ int copy = 0;
+ int move = 0;
+ } counters;
+
+ const Counters& operator()() const { return counters; }
+ };
+
+ F f;
+ std::move_only_function<const F::Counters&() const> m1(f);
+ VERIFY( m1().copy == 1 );
+ VERIFY( m1().move == 0 );
+
+ // This will move construct a new target object and destroy the old one:
+ auto m2 = std::move(m1);
+ VERIFY( m1 == nullptr && m2 != nullptr );
+ VERIFY( m2().copy == 1 );
+ VERIFY( m2().move == 1 );
+
+ m1 = std::move(m2);
+ VERIFY( m1 != nullptr && m2 == nullptr );
+ VERIFY( m1().copy == 1 );
+ VERIFY( m1().move == 2 );
+
+ m2 = std::move(f);
+ VERIFY( m2().copy == 0 );
+ VERIFY( m2().move == 2 ); // Move construct target object, then swap into m2.
+ const int moves = m1().move + m2().move;
+ // This will do three moves:
+ swap(m1, m2);
+ VERIFY( m1().copy == 0 );
+ VERIFY( m2().copy == 1 );
+ VERIFY( (m1().move + m2().move) == (moves + 3) );
+}
+
+void
+test02()
+{
+ // Move constructor is potentially throwing. Allocated on the heap.
+ struct F
+ {
+ F() = default;
+ F(const F& f) noexcept : counters(f.counters) { ++counters.copy; }
+ F(F&& f) noexcept(false) : counters(f.counters) { ++counters.move; }
+
+ F& operator=(F&&) = delete;
+
+ struct Counters
+ {
+ int copy = 0;
+ int move = 0;
+ } counters;
+
+ Counters operator()() const noexcept { return counters; }
+ };
+
+ F f;
+ std::move_only_function<F::Counters() const> m1(f);
+ VERIFY( m1().copy == 1 );
+ VERIFY( m1().move == 0 );
+
+ // The target object is on the heap so this just moves a pointer:
+ auto m2 = std::move(m1);
+ VERIFY( m1 == nullptr && m2 != nullptr );
+ VERIFY( m2().copy == 1 );
+ VERIFY( m2().move == 0 );
+
+ m1 = std::move(m2);
+ VERIFY( m1 != nullptr && m2 == nullptr );
+ VERIFY( m1().copy == 1 );
+ VERIFY( m1().move == 0 );
+
+ m2 = std::move(f);
+ VERIFY( m2().copy == 0 );
+ VERIFY( m2().move == 1 );
+ const int moves = m1().move + m2().move;
+ // This just swaps the pointers, so no moves:
+ swap(m1, m2);
+ VERIFY( m1().copy == 0 );
+ VERIFY( m2().copy == 1 );
+ VERIFY( (m1().move + m2().move) == moves );
+}
+
+int main()
+{
+ test01();
+ test02();
+}
new file mode 100644
@@ -0,0 +1,10 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <version>
+
+#ifndef __cpp_lib_move_only_function
+# error "Feature-test macro for move_only_function missing in <version>"
+#elif __cpp_lib_move_only_function != 202110L
+# error "Feature-test macro for move_only_function has wrong value in <version>"
+#endif