commit 881d1689a42cc7a1fd63bde53c883e52a56eded3 Author: Jonathan Wakely Date: Wed Oct 6 13:28:57 2021 libstdc++: Implement std::move_only_function for C++23 (P0288R9) libstdc++-v3/ChangeLog: * include/Makefile.am: Add new headers. * include/Makefile.in: Regenerate. * include/std/functional: Include . * include/std/version (__cpp_lib_move_only_function): Define. * include/bits/mofunc_impl.h: New file. * include/bits/move_only_function.h: New file. * testsuite/20_util/move_only_function/call.cc: New test. * testsuite/20_util/move_only_function/cons.cc: New test. * testsuite/20_util/move_only_function/move.cc: New test. * testsuite/20_util/move_only_function/version.cc: New test. diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am index 5a5b5fd1d1b..27b548607b9 100644 --- a/libstdc++-v3/include/Makefile.am +++ b/libstdc++-v3/include/Makefile.am @@ -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 \ diff --git a/libstdc++-v3/include/bits/mofunc_impl.h b/libstdc++-v3/include/bits/mofunc_impl.h new file mode 100644 index 00000000000..543c6f547b7 --- /dev/null +++ b/libstdc++-v3/include/bits/mofunc_impl.h @@ -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 +// . + +/** @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 + class move_only_function<_Res(_ArgTypes...) _GLIBCXX_MOF_CV + _GLIBCXX_MOF_REF noexcept(_Noex)> + : _Mofunc_base + { + template + 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 + template + 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> + 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> + || 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 + 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, _Tp>); + _M_init<_Tp>(std::forward<_Args>(__args)...); + } + + template + 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, _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 + requires is_constructible_v + move_only_function& + operator=(_Fn&& __f) + noexcept(is_nothrow_constructible_v) + { + 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 + using __param_t + = __conditional_t + && sizeof(_Tp) <= sizeof(long), + _Tp, _Tp&&>; + + using _Invoker = _Res (*)(_Mofunc_base _GLIBCXX_MOF_CV*, + __param_t<_ArgTypes>...) noexcept(_Noex); + + template + 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 diff --git a/libstdc++-v3/include/bits/move_only_function.h b/libstdc++-v3/include/bits/move_only_function.h new file mode 100644 index 00000000000..f96552a7c89 --- /dev/null +++ b/libstdc++-v3/include/bits/move_only_function.h @@ -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 +// . + +/** @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 +#include + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + +#define __cpp_lib_move_only_function 202110L + + template + 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 + static constexpr bool + _S_nothrow_init() noexcept + { + if constexpr (__stored_locally<_Tp>) + return is_nothrow_constructible_v<_Tp, _Args...>; + return false; + } + + template + 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 + static _Tp* + _S_access(_Self* __self) noexcept + { + if constexpr (__stored_locally>) + 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 + 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 + 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 + inline constexpr bool __is_move_only_function_v = false; + template + constexpr bool __is_move_only_function_v> = 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 diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index a13f67e4c7c..2db1c05349b 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -67,6 +67,9 @@ # include # include #endif +#if __cplusplus > 202002L +# include +#endif #endif // C++11 diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version index 3d4a4142eec..24b86e0fa63 100644 --- a/libstdc++-v3/include/std/version +++ b/libstdc++-v3/include/std/version @@ -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 diff --git a/libstdc++-v3/testsuite/20_util/move_only_function/call.cc b/libstdc++-v3/testsuite/20_util/move_only_function/call.cc new file mode 100644 index 00000000000..9ca0a60a24e --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/move_only_function/call.cc @@ -0,0 +1,199 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include +#include + +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>> ); +static_assert( is_same_v>> ); +static_assert( is_same_v>> ); + +// With const qualifier +static_assert( ! is_invocable_v< move_only_function const > ); +static_assert( ! is_invocable_v< move_only_function const &> ); +static_assert( is_invocable_v< move_only_function > ); +static_assert( is_invocable_v< move_only_function &> ); +static_assert( is_invocable_v< move_only_function const > ); +static_assert( is_invocable_v< move_only_function const &> ); + +// With no ref-qualifier +static_assert( is_invocable_v< move_only_function > ); +static_assert( is_invocable_v< move_only_function &> ); +static_assert( is_invocable_v< move_only_function > ); +static_assert( is_invocable_v< move_only_function &> ); +static_assert( is_invocable_v< move_only_function const > ); +static_assert( is_invocable_v< move_only_function const &> ); + +// With & ref-qualifier +static_assert( ! is_invocable_v< move_only_function > ); +static_assert( is_invocable_v< move_only_function &> ); +static_assert( is_invocable_v< move_only_function > ); +static_assert( is_invocable_v< move_only_function &> ); +static_assert( is_invocable_v< move_only_function const > ); +static_assert( is_invocable_v< move_only_function const &> ); + +// With && ref-qualifier +static_assert( is_invocable_v< move_only_function > ); +static_assert( ! is_invocable_v< move_only_function &> ); +static_assert( is_invocable_v< move_only_function > ); +static_assert( ! is_invocable_v< move_only_function &> ); +static_assert( is_invocable_v< move_only_function const > ); +static_assert( ! is_invocable_v< move_only_function const &> ); + +// With noexcept-specifier +static_assert( ! is_nothrow_invocable_v< move_only_function > ); +static_assert( ! is_nothrow_invocable_v< move_only_function > ); +static_assert( is_nothrow_invocable_v< move_only_function > ); +static_assert( is_nothrow_invocable_v< move_only_function& > ); + +void +test01() +{ + struct F + { + int operator()() { return 0; } + int operator()() const { return 1; } + }; + + move_only_function f0{F{}}; + VERIFY( f0() == 0 ); + VERIFY( std::move(f0)() == 0 ); + + move_only_function 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 f2{F{}}; + VERIFY( f2() == 0 ); + // Not rvalue-callable: std::move(f2)() + + move_only_function 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 f4{F{}}; + // Not lvalue-callable: f4() + VERIFY( std::move(f4)() == 0 ); + + move_only_function 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 f0{F{}}; + VERIFY( f0() == 0 ); + VERIFY( std::move(f0)() == 0 ); + + move_only_function f1{F{}}; + // Not lvalue callable: f1() + VERIFY( std::move(f1)() == 1 ); + + move_only_function 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 f0{F{}}; + VERIFY( f0() == 0 ); + VERIFY( std::move(f0)() == 0 ); + + move_only_function f1{F{}}; + // Not lvalue callable: f1() + VERIFY( std::move(f1)() == 1 ); + + move_only_function 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 f3{F{}}; + // Not lvalue callable: f3() + VERIFY( std::move(f3)() == 0 ); + VERIFY( std::move(std::as_const(f3))() == 0 ); + + move_only_function 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 f0{F{}}; + VERIFY( f0() == 0 ); + VERIFY( std::move(f0)() == 0 ); + + move_only_function f1{F{}}; + VERIFY( f1() == 0 ); + // Not rvalue-callable: std::move(f1)() + + move_only_function f2{F{}}; + // Not lvalue callable: f2() + VERIFY( std::move(f2)() == 1 ); + + move_only_function 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 f4{F{}}; + VERIFY( f4() == 2 ); + VERIFY( std::as_const(f4)() == 2 ); + // Not rvalue-callable: std::move(f4)() + + move_only_function 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(); +} diff --git a/libstdc++-v3/testsuite/20_util/move_only_function/cons.cc b/libstdc++-v3/testsuite/20_util/move_only_function/cons.cc new file mode 100644 index 00000000000..0992f107003 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/move_only_function/cons.cc @@ -0,0 +1,98 @@ +// { dg-options "-std=gnu++23" } +// { dg-do compile { target c++23 } } + +#include + +#ifndef __cpp_lib_move_only_function +# error "Feature-test macro for move_only_function missing in " +#elif __cpp_lib_move_only_function != 202110L +# error "Feature-test macro for move_only_function has wrong value in " +#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> ); +static_assert( is_nothrow_constructible_v, nullptr_t> ); +static_assert( is_nothrow_move_constructible_v> ); +static_assert( ! is_copy_constructible_v> ); + +static_assert( is_constructible_v, void()> ); +static_assert( is_constructible_v, void(&)()> ); +static_assert( is_constructible_v, void(*)()> ); +static_assert( is_constructible_v, int()> ); +static_assert( is_constructible_v, int(&)()> ); +static_assert( is_constructible_v, int(*)()> ); +static_assert( ! is_constructible_v, void(int)> ); +static_assert( is_constructible_v, void(int)> ); + +static_assert( is_constructible_v, + in_place_type_t, void(int)> ); + +static_assert( is_constructible_v, + void() noexcept> ); +static_assert( is_constructible_v, + void() noexcept> ); +static_assert( ! is_constructible_v, + void() > ); + +struct Q +{ + void operator()() const &; + void operator()() &&; +}; + +static_assert( is_constructible_v, Q> ); +static_assert( is_constructible_v, Q> ); +static_assert( is_constructible_v, Q> ); +static_assert( is_constructible_v, Q> ); +static_assert( is_constructible_v, Q> ); +static_assert( is_constructible_v, Q> ); + +struct R +{ + void operator()() &; + void operator()() &&; +}; + +static_assert( is_constructible_v, R> ); +static_assert( is_constructible_v, R> ); +static_assert( is_constructible_v, R> ); +static_assert( ! is_constructible_v, R> ); +static_assert( ! is_constructible_v, R> ); +static_assert( ! is_constructible_v, R> ); + +// The following nothrow-constructible guarantees are a GCC extension, +// not required by the standard. + +static_assert( is_nothrow_constructible_v, void()> ); +static_assert( is_nothrow_constructible_v, + in_place_type_t, + 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, F> ); +static_assert( is_nothrow_constructible_v, G> ); +static_assert( is_nothrow_constructible_v, G> ); + +struct H { + H(int); + H(int, int) noexcept; + void operator()() noexcept; +}; +static_assert( is_nothrow_constructible_v, H> ); +static_assert( is_nothrow_constructible_v, + H> ); +static_assert( ! is_nothrow_constructible_v, + in_place_type_t, int> ); +static_assert( is_nothrow_constructible_v, + in_place_type_t, int, int> ); diff --git a/libstdc++-v3/testsuite/20_util/move_only_function/move.cc b/libstdc++-v3/testsuite/20_util/move_only_function/move.cc new file mode 100644 index 00000000000..f1f0fb597e5 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/move_only_function/move.cc @@ -0,0 +1,109 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include + +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 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 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(); +} diff --git a/libstdc++-v3/testsuite/20_util/move_only_function/version.cc b/libstdc++-v3/testsuite/20_util/move_only_function/version.cc new file mode 100644 index 00000000000..9fe52f46123 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/move_only_function/version.cc @@ -0,0 +1,10 @@ +// { dg-options "-std=gnu++23" } +// { dg-do compile { target c++23 } } + +#include + +#ifndef __cpp_lib_move_only_function +# error "Feature-test macro for move_only_function missing in " +#elif __cpp_lib_move_only_function != 202110L +# error "Feature-test macro for move_only_function has wrong value in " +#endif