diff --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst index d5ed9188b1b23..8182f7d6ccdce 100644 --- a/libcxx/docs/FeatureTestMacroTable.rst +++ b/libcxx/docs/FeatureTestMacroTable.rst @@ -356,7 +356,7 @@ Status ---------------------------------------------------------- ----------------- ``__cpp_lib_modules`` ``202207L`` ---------------------------------------------------------- ----------------- - ``__cpp_lib_move_only_function`` *unimplemented* + ``__cpp_lib_move_only_function`` ``202110L`` ---------------------------------------------------------- ----------------- ``__cpp_lib_optional`` ``202110L`` ---------------------------------------------------------- ----------------- @@ -530,4 +530,3 @@ Status ---------------------------------------------------------- ----------------- ``__cpp_lib_variant`` ``202306L`` ========================================================== ================= - diff --git a/libcxx/docs/ReleaseNotes/22.rst b/libcxx/docs/ReleaseNotes/22.rst index 58e0ee9993065..9b0b2c548ebb8 100644 --- a/libcxx/docs/ReleaseNotes/22.rst +++ b/libcxx/docs/ReleaseNotes/22.rst @@ -48,6 +48,7 @@ Implemented Papers - P2835R7: Expose ``std::atomic_ref``'s object address (`Github `__) - P2944R3: Comparisons for ``reference_wrapper`` (`Github `__) - P3168R2: Give ``std::optional`` Range Support (`Github `__) +- P0288R9 - ``move_only_function`` (`Github `__) Improvements and New Features ----------------------------- diff --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv index b655384bad7f2..1249afe6968ad 100644 --- a/libcxx/docs/Status/Cxx23Papers.csv +++ b/libcxx/docs/Status/Cxx23Papers.csv @@ -23,7 +23,7 @@ "`P2136R3 `__","invoke_r","2021-06 (Virtual)","|Complete|","17","`#105155 `__","" "`P2166R1 `__","A Proposal to Prohibit std::basic_string and std::basic_string_view construction from nullptr","2021-06 (Virtual)","|Complete|","13","`#105156 `__","" "","","","","","","" -"`P0288R9 `__","``any_invocable``","2021-10 (Virtual)","","","`#105157 `__","" +"`P0288R9 `__","``move_only_function``","2021-10 (Virtual)","|Complete|","22","`#105157 `__","" "`P0798R8 `__","Monadic operations for ``std::optional``","2021-10 (Virtual)","|Complete|","14","`#105158 `__","" "`P0849R8 `__","``auto(x)``: ``DECAY_COPY`` in the language","2021-10 (Virtual)","|Complete|","14","`#105159 `__","" "`P1072R10 `__","``basic_string::resize_and_overwrite``","2021-10 (Virtual)","|Complete|","14","`#105160 `__","" diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 57032ce26d4fd..96445cb60d968 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -427,6 +427,9 @@ set(files __functional/is_transparent.h __functional/mem_fn.h __functional/mem_fun_ref.h + __functional/move_only_function.h + __functional/move_only_function_common.h + __functional/move_only_function_impl.h __functional/not_fn.h __functional/operations.h __functional/perfect_forward.h diff --git a/libcxx/include/__configuration/abi.h b/libcxx/include/__configuration/abi.h index 38b85c6ac70de..557cae29d8a0c 100644 --- a/libcxx/include/__configuration/abi.h +++ b/libcxx/include/__configuration/abi.h @@ -83,6 +83,7 @@ # define _LIBCPP_ABI_USE_WRAP_ITER_IN_STD_ARRAY # define _LIBCPP_ABI_USE_WRAP_ITER_IN_STD_STRING_VIEW # define _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION +# define _LIBCPP_ABI_SMALL_BUFFER_TRIVIAL_ABI #elif _LIBCPP_ABI_VERSION == 1 # if !(defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF)) diff --git a/libcxx/include/__functional/move_only_function.h b/libcxx/include/__functional/move_only_function.h new file mode 100644 index 0000000000000..98631f43201ab --- /dev/null +++ b/libcxx/include/__functional/move_only_function.h @@ -0,0 +1,97 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___FUNCTIONAL_MOVE_ONLY_FUNCTION_H +#define _LIBCPP___FUNCTIONAL_MOVE_ONLY_FUNCTION_H + +#include <__config> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER >= 23 && defined(_LIBCPP_ENABLE_EXPERIMENTAL) + +// move_only_function design: +// +// move_only_function has a small buffer with a size of `3 * sizeof(void*)` bytes. This buffer can only be used when the +// object that should be stored is trivially relocatable (currently only when it is trivially move constructible and +// trivially destructible). There is also a bool in the lower bits of the vptr stored which is set when the contained +// object is not trivially destructible. +// +// trivially relocatable: It would also be possible to store nothrow_move_constructible types, but that would mean +// that move_only_function itself would not be trivially relocatable anymore. The decision to keep move_only_function +// trivially relocatable was made because we expect move_only_function to be mostly used to store a functor. To only +// forward functors there is C++26's std::function_ref. +// +// buffer size: We did a survey of six implementations from various vendors. Three of them had a buffer size of 24 bytes +// on 64 bit systems. This also allows storing a std::string or std::vector inside the small buffer (once the compiler +// has full support of trivially_relocatable annotations). +// +// trivially-destructible bit: This allows us to keep the overall binary size smaller because we don't have to store +// a pointer to a noop function inside the vtable. It also avoids loading the vtable during destruction, potentially +// resulting in fewer cache misses. The downside is that calling the function now also requires setting the lower bits +// of the pointer to zero, but this is a very fast operation on modern CPUs. +// +// interaction with copyable_function: When converting a copyable_function into a move_only_function we want to avoid +// wrapping the copyable_function inside the move_only_function to avoid a double indirection. Instead, we copy the +// small buffer and use copyable_function's vtable. + +// NOLINTBEGIN(readability-duplicate-include) +# define _LIBCPP_IN_MOVE_ONLY_FUNCTION_H + +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF & +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF && +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF & +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF && +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF & +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF && +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF & +# include <__functional/move_only_function_impl.h> + +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF && +# include <__functional/move_only_function_impl.h> + +# undef _LIBCPP_IN_MOVE_ONLY_FUNCTION_H +// NOLINTEND(readability-duplicate-include) + +#endif // _LIBCPP_STD_VER >= 23 && defined(_LIBCPP_ENABLE_EXPERIMENTAL) + +#endif // _LIBCPP___FUNCTIONAL_MOVE_ONLY_FUNCTION_H diff --git a/libcxx/include/__functional/move_only_function_common.h b/libcxx/include/__functional/move_only_function_common.h new file mode 100644 index 0000000000000..895b434b7f9be --- /dev/null +++ b/libcxx/include/__functional/move_only_function_common.h @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___FUNCTIONAL_MOVE_ONLY_FUNCTION_COMMON_H +#define _LIBCPP___FUNCTIONAL_MOVE_ONLY_FUNCTION_COMMON_H + +#include <__config> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template +class move_only_function; + +template +inline constexpr bool __is_move_only_function_v = false; + +template +inline constexpr bool __is_move_only_function_v> = true; + +template +struct _MoveOnlyFunctionVTable { + using _CallFunc = _ReturnT(_BufferT&, _ArgTypes...); + using _DestroyFunc = void(_BufferT&) noexcept; + + _CallFunc* __call_; + _DestroyFunc* __destroy_; +}; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___FUNCTIONAL_MOVE_ONLY_FUNCTION_COMMON_H diff --git a/libcxx/include/__functional/move_only_function_impl.h b/libcxx/include/__functional/move_only_function_impl.h new file mode 100644 index 0000000000000..bf3ddf9425676 --- /dev/null +++ b/libcxx/include/__functional/move_only_function_impl.h @@ -0,0 +1,232 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// This header is unguarded on purpose. This header is an implementation detail of move_only_function.h +// and generates multiple versions of std::move_only_function + +#include <__assert> +#include <__config> +#include <__cstddef/nullptr_t.h> +#include <__cstddef/size_t.h> +#include <__functional/invoke.h> +#include <__functional/move_only_function_common.h> +#include <__memory/construct_at.h> +#include <__type_traits/decay.h> +#include <__type_traits/invoke.h> +#include <__type_traits/is_constructible.h> +#include <__type_traits/is_member_pointer.h> +#include <__type_traits/is_pointer.h> +#include <__type_traits/is_trivially_destructible.h> +#include <__type_traits/remove_cvref.h> +#include <__type_traits/remove_pointer.h> +#include <__utility/exchange.h> +#include <__utility/forward.h> +#include <__utility/in_place.h> +#include <__utility/move.h> +#include <__utility/small_buffer.h> +#include <__utility/swap.h> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#ifndef _LIBCPP_IN_MOVE_ONLY_FUNCTION_H +# error This header should only be included from move_only_function.h +#endif + +#ifndef _LIBCPP_MOVE_ONLY_FUNCTION_CV +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV +#endif + +#ifndef _LIBCPP_MOVE_ONLY_FUNCTION_REF +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF +# define _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS _LIBCPP_MOVE_ONLY_FUNCTION_CV& +#else +# define _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS _LIBCPP_MOVE_ONLY_FUNCTION_CV _LIBCPP_MOVE_ONLY_FUNCTION_REF +#endif + +#ifndef _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT false +#endif + +#define _LIBCPP_MOVE_ONLY_FUNCTION_CVREF _LIBCPP_MOVE_ONLY_FUNCTION_CV _LIBCPP_MOVE_ONLY_FUNCTION_REF + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#ifdef _LIBCPP_ABI_MOVE_ONLY_FUNCTION_TRIVIAL_ABI +# define _LIBCPP_MOVE_ONLY_FUNCTION_TRIVIAL_ABI [[_Clang::__trivial_abi__]] +#else +# define _LIBCPP_MOVE_ONLY_FUNCTION_TRIVIAL_ABI +#endif + +template +class move_only_function; + +template +class _LIBCPP_MOVE_ONLY_FUNCTION_TRIVIAL_ABI move_only_function<_ReturnT( + _ArgTypes...) _LIBCPP_MOVE_ONLY_FUNCTION_CVREF noexcept(_LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT)> { +private: + static constexpr size_t __buffer_size_ = 3 * sizeof(void*); + static constexpr size_t __buffer_alignment_ = alignof(void*); + using _BufferT = __small_buffer<__buffer_size_, __buffer_alignment_>; + + using _VTable = _MoveOnlyFunctionVTable<_BufferT, _ReturnT, _ArgTypes...>; + + template + static constexpr _VTable __vtable_var_ = { + .__call_ = [](_BufferT& __buffer, _ArgTypes... __args) noexcept(_LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT) -> _ReturnT { + return std::invoke_r<_ReturnT>( + static_cast<_Functor _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS>(*__buffer.__get<_Functor>()), + std::forward<_ArgTypes>(__args)...); + }, + .__destroy_ = (_BufferT::__fits_in_buffer<_Functor> && is_trivially_destructible_v<_Functor>) + ? nullptr + : [](_BufferT& __buffer) noexcept -> void { + std::destroy_at(__buffer.__get<_Functor>()); + __buffer.__dealloc<_Functor>(); + }}; + + template + static constexpr bool __is_callable_from = [] { + using _DVT = decay_t<_VT>; + if (_LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT) { + return is_nothrow_invocable_r_v<_ReturnT, _DVT _LIBCPP_MOVE_ONLY_FUNCTION_CVREF, _ArgTypes...> && + is_nothrow_invocable_r_v<_ReturnT, _DVT _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS, _ArgTypes...>; + } else { + return is_invocable_r_v<_ReturnT, _DVT _LIBCPP_MOVE_ONLY_FUNCTION_CVREF, _ArgTypes...> && + is_invocable_r_v<_ReturnT, _DVT _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS, _ArgTypes...>; + } + }(); + + template + _LIBCPP_HIDE_FROM_ABI void __construct(_Args&&... __args) { + static_assert(is_constructible_v, _Func>); + + using _StoredFunc = decay_t<_Func>; + __vtable_ = std::addressof(__vtable_var_<_StoredFunc>); + __buffer_.__construct<_StoredFunc>(std::forward<_Args>(__args)...); + } + + _LIBCPP_HIDE_FROM_ABI void __reset() noexcept { + if (__vtable_ && __vtable_->__destroy_) + __vtable_->__destroy_(__buffer_); + __vtable_ = {}; + } + +public: + using result_type = _ReturnT; + + // [func.wrap.move.ctor] + move_only_function() noexcept = default; + _LIBCPP_HIDE_FROM_ABI move_only_function(nullptr_t) noexcept {} + _LIBCPP_HIDE_FROM_ABI move_only_function(move_only_function&& __other) noexcept + : __vtable_(__other.__vtable_), __buffer_(std::move(__other.__buffer_)) { + __other.__vtable_ = {}; + } + + template + requires(!is_same_v, move_only_function> && !__is_inplace_type<_Func>::value && + __is_callable_from<_Func>) + _LIBCPP_HIDE_FROM_ABI move_only_function(_Func&& __func) noexcept { + using _StoredFunc = decay_t<_Func>; + + if constexpr ((is_pointer_v<_StoredFunc> && is_function_v>) || + is_member_function_pointer_v<_StoredFunc>) { + if (__func != nullptr) { + __vtable_ = std::addressof(__vtable_var_<_StoredFunc>); + static_assert(_BufferT::__fits_in_buffer<_StoredFunc>); + __buffer_.__construct<_StoredFunc>(std::forward<_Func>(__func)); + } + } else if constexpr (__is_move_only_function_v<_StoredFunc>) { + if (__func) { + __vtable_ = std::exchange(__func.__vtable_, {}); + __buffer_ = std::move(__func.__buffer_); + } + } else { + __construct<_Func>(std::forward<_Func>(__func)); + } + } + + template + requires is_constructible_v, _Args...> && __is_callable_from<_Func> + _LIBCPP_HIDE_FROM_ABI explicit move_only_function(in_place_type_t<_Func>, _Args&&... __args) { + static_assert(is_same_v, _Func>); + __construct<_Func>(std::forward<_Args>(__args)...); + } + + template + requires is_constructible_v, initializer_list<_InitListType>&, _Args...> && __is_callable_from<_Func> + _LIBCPP_HIDE_FROM_ABI explicit move_only_function( + in_place_type_t<_Func>, initializer_list<_InitListType> __il, _Args&&... __args) { + static_assert(is_same_v, _Func>); + __construct<_Func>(__il, std::forward<_Args>(__args)...); + } + + _LIBCPP_HIDE_FROM_ABI move_only_function& operator=(move_only_function&& __other) noexcept { + move_only_function(std::move(__other)).swap(*this); + return *this; + } + + _LIBCPP_HIDE_FROM_ABI move_only_function& operator=(nullptr_t) noexcept { + __reset(); + return *this; + } + + template + requires(!is_same_v, move_only_function> && !__is_inplace_type<_Func>::value && + __is_callable_from<_Func>) + _LIBCPP_HIDE_FROM_ABI move_only_function& operator=(_Func&& __func) { + move_only_function(std::forward<_Func>(__func)).swap(*this); + return *this; + } + + _LIBCPP_HIDE_FROM_ABI ~move_only_function() { __reset(); } + + // [func.wrap.move.inv] + _LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { return __vtable_; } + + _LIBCPP_HIDE_FROM_ABI _ReturnT operator()(_ArgTypes... __args) _LIBCPP_MOVE_ONLY_FUNCTION_CVREF + noexcept(_LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT) { + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(static_cast(*this), "Tried to call a disengaged move_only_function"); + const auto __call = static_cast<_ReturnT (*)(_BufferT&, _ArgTypes...)>(__vtable_->__call_); + return __call(__buffer_, std::forward<_ArgTypes>(__args)...); + } + + // [func.wrap.move.util] + _LIBCPP_HIDE_FROM_ABI void swap(move_only_function& __other) noexcept { + std::swap(__vtable_, __other.__vtable_); + std::swap(__buffer_, __other.__buffer_); + } + + _LIBCPP_HIDE_FROM_ABI friend void swap(move_only_function& __lhs, move_only_function& __rhs) noexcept { + __lhs.swap(__rhs); + } + + _LIBCPP_HIDE_FROM_ABI friend bool operator==(const move_only_function& __func, nullptr_t) noexcept { return !__func; } + +private: + const _VTable* __vtable_ = nullptr; + mutable _BufferT __buffer_; + + template + friend class move_only_function; +}; + +#undef _LIBCPP_MOVE_ONLY_FUNCTION_CV +#undef _LIBCPP_MOVE_ONLY_FUNCTION_REF +#undef _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT +#undef _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS +#undef _LIBCPP_MOVE_ONLY_FUNCTION_CVREF + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS diff --git a/libcxx/include/functional b/libcxx/include/functional index 9ebcd818ec840..3dcc1c25ad9f1 100644 --- a/libcxx/include/functional +++ b/libcxx/include/functional @@ -569,6 +569,10 @@ POLICY: For non-variadic implementations, the number of arguments is limited # include <__type_traits/unwrap_ref.h> # endif +# if _LIBCPP_STD_VER >= 23 +# include <__functional/move_only_function.h> +# endif + # include # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in index 24a2fe761943a..453b2bc2044c4 100644 --- a/libcxx/include/module.modulemap.in +++ b/libcxx/include/module.modulemap.in @@ -1425,6 +1425,9 @@ module std [system] { module is_transparent { header "__functional/is_transparent.h" } module mem_fn { header "__functional/mem_fn.h" } module mem_fun_ref { header "__functional/mem_fun_ref.h" } + module move_only_function { header "__functional/move_only_function.h" } + module move_only_function_common { header "__functional/move_only_function_common.h" } + module move_only_function_impl { textual header "__functional/move_only_function_impl.h" } module not_fn { header "__functional/not_fn.h" export std.functional.perfect_forward // inherited from and using its operators diff --git a/libcxx/include/version b/libcxx/include/version index b0030602f854a..20e9afd455543 100644 --- a/libcxx/include/version +++ b/libcxx/include/version @@ -510,7 +510,7 @@ __cpp_lib_void_t 201411L # define __cpp_lib_is_scoped_enum 202011L # define __cpp_lib_mdspan 202207L # define __cpp_lib_modules 202207L -// # define __cpp_lib_move_only_function 202110L +# define __cpp_lib_move_only_function 202110L # undef __cpp_lib_optional # define __cpp_lib_optional 202110L # define __cpp_lib_out_ptr 202106L diff --git a/libcxx/test/libcxx/utilities/function.objects/func.wrap/func.wrap.move/assert.engaged.cpp b/libcxx/test/libcxx/utilities/function.objects/func.wrap/func.wrap.move/assert.engaged.cpp new file mode 100644 index 0000000000000..910e83c625faa --- /dev/null +++ b/libcxx/test/libcxx/utilities/function.objects/func.wrap/func.wrap.move/assert.engaged.cpp @@ -0,0 +1,20 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 +// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 + +#include + +#include "check_assertion.h" + +int main(int, char**) { + std::move_only_function func; + TEST_LIBCPP_ASSERT_FAILURE(func(), "Tried to call a disengaged move_only_function"); +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/functor.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/functor.pass.cpp new file mode 100644 index 0000000000000..0598da573313b --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/functor.pass.cpp @@ -0,0 +1,84 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include +#include +#include + +#include "type_algorithms.h" +#include "../common.h" + +template +void test() { + static_assert(!std::is_nothrow_assignable_v&, NonTrivial>); + { + std::move_only_function f; + std::same_as&> decltype(auto) ret = (f = &call_func); + assert(&ret == &f); + assert(f); + } + { + std::move_only_function f; + decltype(&call_func) ptr = nullptr; + std::same_as&> decltype(auto) ret = (f = ptr); + assert(&ret == &f); + assert(!f); + } + { + std::move_only_function f; + std::same_as&> decltype(auto) ret = (f = TriviallyDestructible{}); + assert(&ret == &f); + assert(f); + } + { + std::move_only_function f; + std::same_as&> decltype(auto) ret = (f = TriviallyDestructibleTooLarge{}); + assert(&ret == &f); + assert(f); + } + { + std::move_only_function f; + std::same_as&> decltype(auto) ret = (f = NonTrivial{}); + assert(&ret == &f); + assert(f); + } +} + +struct S { + void func() noexcept {} +}; + +template +void test_member_function_pointer() { + { + std::move_only_function f = &S::func; + std::move_only_function f2; + f2 = std::move(f); + assert(f2); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + std::move_only_function f2; + f2 = std::move(f); + assert(!f2); + } +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { + test_member_function_pointer(); + }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/move.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/move.pass.cpp new file mode 100644 index 0000000000000..1f17d991e8c4f --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/move.pass.cpp @@ -0,0 +1,91 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include +#include + +#include "test_macros.h" +#include "type_algorithms.h" +#include "../common.h" + +template +void test() { + static_assert(!std::is_assignable_v, int>); + { + std::move_only_function f = &call_func; + std::move_only_function f2; + f2 = std::move(f); + assert(f2); + LIBCPP_ASSERT(!f); + } + { + decltype(&call_func) ptr = nullptr; + std::move_only_function f = ptr; + std::move_only_function f2; + f2 = std::move(f); + assert(!f2); + LIBCPP_ASSERT(!f); + } + { + std::move_only_function f = TriviallyDestructible{}; + std::move_only_function f2; + f2 = std::move(f); + assert(f2); + LIBCPP_ASSERT(!f); + } + { + std::move_only_function f = TriviallyDestructibleTooLarge{}; + std::move_only_function f2; + f2 = std::move(f); + assert(f2); + LIBCPP_ASSERT(!f); + } + { + std::move_only_function f = NonTrivial{}; + std::move_only_function f2; + f2 = std::move(f); + assert(f2); + LIBCPP_ASSERT(!f); + } +} + +struct S { + void func() noexcept {} +}; + +template +void test_member_function_pointer() { + { + std::move_only_function f = &S::func; + std::move_only_function f2; + f2 = std::move(f); + assert(f2); + LIBCPP_ASSERT(!f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + std::move_only_function f2; + f2 = std::move(f); + assert(!f2); + LIBCPP_ASSERT(!f); + } +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { + test_member_function_pointer(); + }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/move_other.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/move_other.pass.cpp new file mode 100644 index 0000000000000..b15e9337a1aaa --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/move_other.pass.cpp @@ -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 +#include +#include +#include + +#include "type_algorithms.h" +#include "../common.h" + +template +void test() { + { + std::move_only_function f1; + std::move_only_function f2; + std::same_as&> decltype(auto) ret = (f2 = std::move(f1)); + assert(&ret == &f2); + assert(!f2); + } + { + std::move_only_function f1; + std::move_only_function f2; + std::same_as&> decltype(auto) ret = (f2 = std::move(f1)); + assert(&ret == &f2); + assert(!f2); + } +} + +template +void test2() { + { + std::move_only_function f1 = [] noexcept { return 109; }; + std::move_only_function f2; + std::same_as&> decltype(auto) ret = (f2 = std::move(f1)); + assert(&ret == &f2); + assert(f2); + assert(f2() == 109); + } + { + std::move_only_function f1 = [] noexcept { return 109; }; + std::move_only_function f2; + std::same_as&> 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{}, [] { test(); }); + types::for_each(types::function_noexcept_const_lvalue_ref_qualified{}, [] { test2(); }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/nullptr.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/nullptr.pass.cpp new file mode 100644 index 0000000000000..e32ace99f17f8 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/assignment/nullptr.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include + +#include "type_algorithms.h" +#include "../common.h" + +template +void test() { + { + std::move_only_function f = &call_func; + f = nullptr; + assert(!f); + } + { + decltype(&call_func) ptr = nullptr; + std::move_only_function f = ptr; + f = nullptr; + assert(!f); + } + { + std::move_only_function f = TriviallyDestructible{}; + f = nullptr; + assert(!f); + } + { + std::move_only_function f = TriviallyDestructibleTooLarge{}; + f = nullptr; + assert(!f); + } + { + std::move_only_function f = NonTrivial{}; + f = nullptr; + assert(!f); + } +} + +struct S { + void func() noexcept {} +}; + +template +void test_member_function_pointer() { + { + std::move_only_function f = &S::func; + f = nullptr; + assert(!f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + f = nullptr; + assert(!f); + } +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { + test_member_function_pointer(); + }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue.pass.cpp new file mode 100644 index 0000000000000..0f50f827c4d2a --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue.pass.cpp @@ -0,0 +1,102 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() & {} +}; + +static_assert(std::is_invocable_v&>); +static_assert(!std::is_invocable_v>); +static_assert(!std::is_invocable_v&&>); +static_assert(!std::is_invocable_v const&>); +static_assert(!std::is_invocable_v const >); +static_assert(!std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + f(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeChecker{&type}; + f(); + assert(type == CallType::LValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(f(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue_const.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue_const.pass.cpp new file mode 100644 index 0000000000000..82e2971fce287 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue_const.pass.cpp @@ -0,0 +1,118 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() const& {} +}; + +static_assert(std::is_invocable_v&>); +static_assert(std::is_invocable_v>); +static_assert(std::is_invocable_v&&>); +static_assert(std::is_invocable_v const&>); +static_assert(std::is_invocable_v const >); +static_assert(std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + f(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeChecker{&type}; + f(); + assert(type == CallType::ConstLValue); + } + { + CallType type; + std::move_only_function f = CallTypeChecker{&type}; + f(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::as_const(f)(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::move(f)(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::move(std::as_const(f))(); + assert(type == CallType::ConstLValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(f(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue_const_noexcept.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue_const_noexcept.pass.cpp new file mode 100644 index 0000000000000..d8129f541fc4f --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue_const_noexcept.pass.cpp @@ -0,0 +1,113 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() const& noexcept {} +}; + +static_assert(std::is_invocable_v&>); +static_assert(std::is_invocable_v>); +static_assert(std::is_invocable_v&&>); +static_assert(std::is_invocable_v const&>); +static_assert(std::is_invocable_v const >); +static_assert(std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + f(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeCheckerNoexcept{&type}; + f(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::as_const(f)(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::move(f)(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::move(std::as_const(f))(); + assert(type == CallType::ConstLValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(f(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue_noexcept.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue_noexcept.pass.cpp new file mode 100644 index 0000000000000..d14f527f812e1 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/lvalue_noexcept.pass.cpp @@ -0,0 +1,102 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() & noexcept {} +}; + +static_assert(std::is_invocable_v&>); +static_assert(!std::is_invocable_v>); +static_assert(!std::is_invocable_v&&>); +static_assert(!std::is_invocable_v const&>); +static_assert(!std::is_invocable_v const >); +static_assert(!std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + f(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeCheckerNoexcept{&type}; + f(); + assert(type == CallType::LValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(f(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal.pass.cpp new file mode 100644 index 0000000000000..a1fe412efaa7f --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal.pass.cpp @@ -0,0 +1,106 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() {} +}; + +static_assert(std::is_invocable_v&>); +static_assert(std::is_invocable_v>); +static_assert(std::is_invocable_v&&>); +static_assert(!std::is_invocable_v const&>); +static_assert(!std::is_invocable_v const >); +static_assert(!std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + f(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeChecker{&type}; + f(); + assert(type == CallType::LValue); + type = CallType::None; + std::move(f)(); + assert(type == CallType::LValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(f(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal_const.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal_const.pass.cpp new file mode 100644 index 0000000000000..8d7bc198b42ad --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal_const.pass.cpp @@ -0,0 +1,112 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() const noexcept {} +}; + +static_assert(std::is_invocable_v&>); +static_assert(std::is_invocable_v>); +static_assert(std::is_invocable_v&&>); +static_assert(std::is_invocable_v const&>); +static_assert(std::is_invocable_v const >); +static_assert(std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + f(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeChecker{&type}; + f(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::as_const(f)(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::move(f)(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::move(std::as_const(f))(); + assert(type == CallType::ConstLValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(f(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal_const_noexcept.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal_const_noexcept.pass.cpp new file mode 100644 index 0000000000000..1c0d18db15430 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal_const_noexcept.pass.cpp @@ -0,0 +1,112 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() const noexcept {} +}; + +static_assert(std::is_invocable_v&>); +static_assert(std::is_invocable_v>); +static_assert(std::is_invocable_v&&>); +static_assert(std::is_invocable_v const&>); +static_assert(std::is_invocable_v const >); +static_assert(std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + f(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeCheckerNoexcept{&type}; + f(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::as_const(f)(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::move(f)(); + assert(type == CallType::ConstLValue); + type = CallType::None; + std::move(std::as_const(f))(); + assert(type == CallType::ConstLValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(f(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal_noexcept.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal_noexcept.pass.cpp new file mode 100644 index 0000000000000..575bc27a35ab3 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/normal_noexcept.pass.cpp @@ -0,0 +1,106 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() noexcept {} +}; + +static_assert(std::is_invocable_v&>); +static_assert(std::is_invocable_v>); +static_assert(std::is_invocable_v&&>); +static_assert(!std::is_invocable_v const&>); +static_assert(!std::is_invocable_v const >); +static_assert(!std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + f(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + f(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeCheckerNoexcept{&type}; + f(); + assert(type == CallType::LValue); + type = CallType::None; + std::move(f)(); + assert(type == CallType::LValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(f(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(f(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue.pass.cpp new file mode 100644 index 0000000000000..f51b48564dd33 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue.pass.cpp @@ -0,0 +1,104 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() && noexcept {} +}; + +static_assert(!std::is_invocable_v&>); +static_assert(std::is_invocable_v>); +static_assert(std::is_invocable_v&&>); +static_assert(!std::is_invocable_v const&>); +static_assert(!std::is_invocable_v const >); +static_assert(!std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + std::move(f)(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeChecker{&type}; + type = CallType::None; + std::move(f)(); + assert(type == CallType::RValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(std::move(f)(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue_const.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue_const.pass.cpp new file mode 100644 index 0000000000000..fc664525d171a --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue_const.pass.cpp @@ -0,0 +1,107 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() const&& noexcept {} +}; + +static_assert(!std::is_invocable_v&>); +static_assert(std::is_invocable_v>); +static_assert(std::is_invocable_v&&>); +static_assert(!std::is_invocable_v const&>); +static_assert(std::is_invocable_v const >); +static_assert(std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + std::move(f)(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeChecker{&type}; + type = CallType::None; + std::move(f)(); + assert(type == CallType::ConstRValue); + type = CallType::None; + std::move(std::as_const(f))(); + assert(type == CallType::ConstRValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(std::move(f)(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue_const_noexcept.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue_const_noexcept.pass.cpp new file mode 100644 index 0000000000000..245b60eb45452 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue_const_noexcept.pass.cpp @@ -0,0 +1,107 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() const&& noexcept {} +}; + +static_assert(!std::is_invocable_v&>); +static_assert(std::is_invocable_v>); +static_assert(std::is_invocable_v&&>); +static_assert(!std::is_invocable_v const&>); +static_assert(std::is_invocable_v const >); +static_assert(std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + std::move(f)(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeCheckerNoexcept{&type}; + type = CallType::None; + std::move(f)(); + assert(type == CallType::ConstRValue); + type = CallType::None; + std::move(std::as_const(f))(); + assert(type == CallType::ConstRValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(std::move(f)(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue_noexcept.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue_noexcept.pass.cpp new file mode 100644 index 0000000000000..87201aeec85cb --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/call/rvalue_noexcept.pass.cpp @@ -0,0 +1,104 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "../common.h" + +struct S { + void func() && noexcept {} +}; + +static_assert(!std::is_invocable_v&>); +static_assert(std::is_invocable_v>); +static_assert(std::is_invocable_v&&>); +static_assert(!std::is_invocable_v const&>); +static_assert(!std::is_invocable_v const >); +static_assert(!std::is_invocable_v const&&>); + +void test() { + { + called = false; + std::move_only_function f = &call_func; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + std::move(f)(); + assert(called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + std::move(f)(); + assert(called); + } + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + CallType type; + std::move_only_function f = CallTypeCheckerNoexcept{&type}; + type = CallType::None; + std::move(f)(); + assert(type == CallType::RValue); + } +} + +void test_return() { + { + called = false; + std::move_only_function f = &get_val; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructible{}; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(std::move(f)(3) == 3); + assert(!called); + } + { + called = false; + std::move_only_function f = NonTrivial{}; + assert(std::move(f)(3) == 3); + assert(!called); + } +} + +int main(int, char**) { + test(); + test_return(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/common.h b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/common.h new file mode 100644 index 0000000000000..e0d82d9b00499 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/common.h @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef MOVE_ONLY_FUNCTION_COMMON_H +#define MOVE_ONLY_FUNCTION_COMMON_H + +#include +#include + +inline bool called; +inline void call_func() noexcept { called = true; } + +struct MoveCounter { + int* counter_; + MoveCounter(int* counter) : counter_(counter) {} + MoveCounter(MoveCounter&& other) : counter_(other.counter_) { ++*counter_; } +}; + +struct TriviallyDestructible { + TriviallyDestructible() = default; + TriviallyDestructible(MoveCounter) {} + TriviallyDestructible(std::initializer_list, MoveCounter) {} + void operator()() const noexcept { called = true; } + int operator()(int i) const noexcept { return i; } +}; + +struct TriviallyDestructibleTooLarge { + TriviallyDestructibleTooLarge() = default; + TriviallyDestructibleTooLarge(MoveCounter) {} + TriviallyDestructibleTooLarge(std::initializer_list, MoveCounter) {} + void operator()() const noexcept { called = true; } + int operator()(int i) const noexcept { return i; } + char a[5 * sizeof(void*)]; +}; + +struct NonTrivial { + NonTrivial() = default; + NonTrivial(MoveCounter) {} + NonTrivial(std::initializer_list&, MoveCounter) {} + NonTrivial(NonTrivial&&) noexcept(false) {} + ~NonTrivial() {} + + void operator()() const noexcept { called = true; } + int operator()(int i) const noexcept { return i; } +}; + +inline int get_val(int i) noexcept { return i; } + +enum class CallType { + None, + LValue, + RValue, + ConstLValue, + ConstRValue, +}; + +struct CallTypeChecker { + CallType* type; + using enum CallType; + void operator()() & { *type = LValue; } + void operator()() && { *type = RValue; } + void operator()() const& { *type = ConstLValue; } + void operator()() const&& { *type = ConstRValue; } +}; + +struct CallTypeCheckerNoexcept { + CallType* type; + using enum CallType; + void operator()() & noexcept { *type = LValue; } + void operator()() && noexcept { *type = RValue; } + void operator()() const& noexcept { *type = ConstLValue; } + void operator()() const&& noexcept { *type = ConstRValue; } +}; + +#endif // MOVE_ONLY_FUNCTION_COMMON_H diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/default.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/default.pass.cpp new file mode 100644 index 0000000000000..d2490628a4bfc --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/default.pass.cpp @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include + +#include "type_algorithms.h" + +template +void test() { + std::move_only_function f; + assert(!f); +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/functor.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/functor.pass.cpp new file mode 100644 index 0000000000000..ef38e80159af8 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/functor.pass.cpp @@ -0,0 +1,115 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include + +#include "count_new.h" +#include "test_macros.h" +#include "type_algorithms.h" +#include "../common.h" + +template +void test() { + { + std::move_only_function f = &call_func; + assert(f); + } + { + decltype(&call_func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + std::move_only_function f = TriviallyDestructible{}; + assert(f); + } + { + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(f); + } + { + std::move_only_function f = NonTrivial{}; + assert(f); + } +} + +struct S { + void func() noexcept {} +}; + +template +void test_member_function_pointer() { + { + std::move_only_function f = &S::func; + assert(f); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } +} + +template +void test_value_return_type() { + { + std::move_only_function f = &get_val; + assert(f); + } + { + decltype(&get_val) ptr = nullptr; + std::move_only_function f = ptr; + assert(!f); + } + { + std::move_only_function f = TriviallyDestructible{}; + assert(f); + } + { + std::move_only_function f = TriviallyDestructibleTooLarge{}; + assert(f); + } + { + std::move_only_function f = NonTrivial{}; + assert(f); + } +} + +template +void test_throwing() { + struct ThrowingFunctor { + ThrowingFunctor() = default; + ThrowingFunctor(const ThrowingFunctor&) { throw 1; } + void operator()() {} + }; + std::move_only_function func({}); +} + +void check_new_delete_called() { + assert(globalMemCounter.new_called == globalMemCounter.delete_called); + assert(globalMemCounter.new_array_called == globalMemCounter.delete_array_called); + assert(globalMemCounter.aligned_new_called == globalMemCounter.aligned_delete_called); + assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called); +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { + test_member_function_pointer(); + }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { + test_value_return_type(); + }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test_throwing(); }); + check_new_delete_called(); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/in_place.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/in_place.pass.cpp new file mode 100644 index 0000000000000..9e415a9786633 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/in_place.pass.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "type_algorithms.h" +#include "../common.h" + +template +void test() { + { + int counter = 0; + std::move_only_function f{std::in_place_type, MoveCounter{&counter}}; + assert(f); + assert(counter == 1); + } + { + int counter = 0; + std::move_only_function f{std::in_place_type, MoveCounter{&counter}}; + assert(f); + assert(counter == 1); + } + { + int counter = 0; + std::move_only_function f{std::in_place_type, MoveCounter{&counter}}; + assert(f); + assert(counter == 1); + } +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/in_place_init_list.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/in_place_init_list.pass.cpp new file mode 100644 index 0000000000000..46c16e24f5ffd --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/in_place_init_list.pass.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "type_algorithms.h" +#include "../common.h" + +template +void test() { + { + int counter = 0; + std::move_only_function f{std::in_place_type, {1}, MoveCounter{&counter}}; + assert(f); + assert(counter == 1); + } + { + int counter = 0; + std::move_only_function f{std::in_place_type, {1}, MoveCounter{&counter}}; + assert(f); + assert(counter == 1); + } + { + int counter = 0; + std::move_only_function f{std::in_place_type, {1}, MoveCounter{&counter}}; + assert(f); + assert(counter == 1); + } +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/move.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/move.pass.cpp new file mode 100644 index 0000000000000..34688b252ab48 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/move.pass.cpp @@ -0,0 +1,106 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "test_macros.h" +#include "type_algorithms.h" +#include "../common.h" + +template +void test() { + { + std::move_only_function f = &call_func; + std::move_only_function f2 = std::move(f); + assert(f2); + } + { + decltype(&call_func) ptr = nullptr; + std::move_only_function f = ptr; + std::move_only_function f2 = std::move(f); + assert(!f2); + } + { + std::move_only_function f = TriviallyDestructible{}; + std::move_only_function f2 = std::move(f); + assert(f2); + } + { + std::move_only_function f = TriviallyDestructibleTooLarge{}; + std::move_only_function f2 = std::move(f); + assert(f2); + } + { + std::move_only_function f = NonTrivial{}; + std::move_only_function f2 = std::move(f); + assert(f2); + } +} + +struct S { + void func() noexcept {} +}; + +template +void test_member_function_pointer() { + { + std::move_only_function f = &S::func; + std::move_only_function f2 = std::move(f); + assert(f2); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + std::move_only_function f2 = std::move(f); + assert(!f2); + } +} + +template +void test_value() { + { + std::move_only_function f = &get_val; + std::move_only_function f2 = std::move(f); + assert(f2); + } + { + decltype(&get_val) ptr = nullptr; + std::move_only_function f = ptr; + std::move_only_function f2 = std::move(f); + assert(!f2); + } + { + std::move_only_function f = TriviallyDestructible{}; + std::move_only_function f2 = std::move(f); + assert(f2); + } + { + std::move_only_function f = TriviallyDestructibleTooLarge{}; + std::move_only_function f2 = std::move(f); + assert(f2); + } + { + std::move_only_function f = NonTrivial{}; + std::move_only_function f2 = std::move(f); + assert(f2); + } +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { + test_member_function_pointer(); + }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test_value(); }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/move_other.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/move_other.pass.cpp new file mode 100644 index 0000000000000..805775579bd38 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/move_other.pass.cpp @@ -0,0 +1,44 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include +#include + +#include "type_algorithms.h" + +static_assert(!std::is_constructible_v, std::move_only_function>); +static_assert(!std::is_constructible_v, std::move_only_function>); +static_assert( + !std::is_constructible_v, std::move_only_function>); +static_assert( + !std::is_constructible_v, std::move_only_function>); +static_assert( + !std::is_constructible_v, std::move_only_function>); + +template +void test() { + { + std::move_only_function f1; + std::move_only_function f2 = std::move(f1); + assert(!f2); + } + { + std::move_only_function f1; + std::move_only_function f2 = std::move(f1); + assert(!f2); + } +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/nullptr.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/nullptr.pass.cpp new file mode 100644 index 0000000000000..1ab95f271b4bc --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/ctors/nullptr.pass.cpp @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include + +#include "type_algorithms.h" + +template +void test() { + std::move_only_function f = nullptr; + assert(!f); +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/swap.adl.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/swap.adl.pass.cpp new file mode 100644 index 0000000000000..d9200dfd3a1dc --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/swap.adl.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include + +#include "type_algorithms.h" +#include "common.h" + +template +void test() { + { + std::move_only_function f = &call_func; + std::move_only_function f2; + swap(f, f2); + } + { + decltype(&call_func) ptr = nullptr; + std::move_only_function f = ptr; + std::move_only_function f2; + swap(f, f2); + } + { + std::move_only_function f = TriviallyDestructible{}; + std::move_only_function f2; + swap(f, f2); + } + { + std::move_only_function f = TriviallyDestructibleTooLarge{}; + std::move_only_function f2; + swap(f, f2); + } + { + std::move_only_function f = NonTrivial{}; + std::move_only_function f2; + swap(f, f2); + } +} + +struct S { + void func() noexcept {} +}; + +template +void test_member_function_pointer() { + { + std::move_only_function f = &S::func; + std::move_only_function f2; + swap(f, f2); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + std::move_only_function f2; + swap(f, f2); + } +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { + test_member_function_pointer(); + }); + + return 0; +} diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/swap.member.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/swap.member.pass.cpp new file mode 100644 index 0000000000000..b14769dcc1223 --- /dev/null +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.move/swap.member.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include + +#include "type_algorithms.h" +#include "common.h" + +template +void test() { + { + std::move_only_function f = &call_func; + std::move_only_function f2; + f.swap(f2); + } + { + decltype(&call_func) ptr = nullptr; + std::move_only_function f = ptr; + std::move_only_function f2; + f.swap(f2); + } + { + std::move_only_function f = TriviallyDestructible{}; + std::move_only_function f2; + f.swap(f2); + } + { + std::move_only_function f = TriviallyDestructibleTooLarge{}; + std::move_only_function f2; + f.swap(f2); + } + { + std::move_only_function f = NonTrivial{}; + std::move_only_function f2; + f.swap(f2); + } +} + +struct S { + void func() noexcept {} +}; + +template +void test_member_function_pointer() { + { + std::move_only_function f = &S::func; + std::move_only_function f2; + f.swap(f2); + } + { + decltype(&S::func) ptr = nullptr; + std::move_only_function f = ptr; + std::move_only_function f2; + f.swap(f2); + } +} + +int main(int, char**) { + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { test(); }); + types::for_each(types::function_noexcept_const_ref_qualified{}, [] { + test_member_function_pointer(); + }); + + return 0; +} diff --git a/libcxx/test/support/type_algorithms.h b/libcxx/test/support/type_algorithms.h index da3d0add4d0c4..b6f65ebb8d716 100644 --- a/libcxx/test/support/type_algorithms.h +++ b/libcxx/test/support/type_algorithms.h @@ -144,6 +144,42 @@ struct type_list_as_pointers > { template using as_pointers = typename type_list_as_pointers::type; +template +struct function_noexcept_const_lvalue_ref_qualified_impl; + +template +struct function_noexcept_const_lvalue_ref_qualified_impl { + using type = + type_list; +}; + +template +using function_noexcept_const_lvalue_ref_qualified = + typename function_noexcept_const_lvalue_ref_qualified_impl::type; + +template +struct function_noexcept_const_ref_qualified_impl; + +template +struct function_noexcept_const_ref_qualified_impl { + using type = + concatenate_t, + type_list >; +}; + +template +using function_noexcept_const_ref_qualified = typename function_noexcept_const_ref_qualified_impl::type; + } // namespace types #endif // TEST_SUPPORT_TYPE_ALGORITHMS_H diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py index 22209f53d50d7..6028f9cb15ed1 100644 --- a/libcxx/utils/generate_feature_test_macro_components.py +++ b/libcxx/utils/generate_feature_test_macro_components.py @@ -973,7 +973,6 @@ def add_version_header(tc): "name": "__cpp_lib_move_only_function", "values": {"c++23": 202110}, "headers": ["functional"], - "unimplemented": True, }, { "name": "__cpp_lib_node_extract",