[libc++] Implement P0798R8 (Monadic operations for std::optional)

Implement P0798R8

Reviewed By: #libc, ldionne, Quuxplusone

Spies: tcanens, Quuxplusone, ldionne, Wmbat, arichardson, Mordante, libcxx-commits

Differential Revision: https://reviews.llvm.org/D113408
This commit is contained in:
Nikolas Klauser 2021-12-15 20:54:24 +01:00
parent cea3638812
commit 17cfc57d14
10 changed files with 768 additions and 3 deletions

View File

@ -298,6 +298,8 @@ Status
------------------------------------------------- -----------------
``__cpp_lib_is_scoped_enum`` ``202011L``
------------------------------------------------- -----------------
``__cpp_lib_monadic_optional`` ``202110L``
------------------------------------------------- -----------------
``__cpp_lib_stacktrace`` *unimplemented*
------------------------------------------------- -----------------
``__cpp_lib_stdatomic_h`` *unimplemented*

View File

@ -24,7 +24,7 @@
"`P2166R1 <https://wg21.link/P2166R1>`__","LWG","A Proposal to Prohibit std::basic_string and std::basic_string_view construction from nullptr","June 2021","|Complete|","13.0"
"","","","","",""
"`P0288R9 <https://wg21.link/P0288R9>`__","LWG","``any_invocable``","October 2021","",""
"`P0798R8 <https://wg21.link/P0798R8>`__","LWG","Monadic operations for ``std::optional``","October 2021","",""
"`P0798R8 <https://wg21.link/P0798R8>`__","LWG","Monadic operations for ``std::optional``","October 2021","|Complete|","14.0"
"`P0849R8 <https://wg21.link/P0849R8>`__","LWG","``auto(x)``: ``DECAY_COPY`` in the language","October 2021","",""
"`P1072R10 <https://wg21.link/P1072R10>`__","LWG","``basic_string::resize_and_overwrite``","October 2021","",""
"`P1147R1 <https://wg21.link/P1147R1>`__","LWG","Printing ``volatile`` Pointers","October 2021","|Complete|","14.0"

1 Paper # Group Paper Name Meeting Status First released version
24 `P2166R1 <https://wg21.link/P2166R1>`__ LWG A Proposal to Prohibit std::basic_string and std::basic_string_view construction from nullptr June 2021 |Complete| 13.0
25
26 `P0288R9 <https://wg21.link/P0288R9>`__ LWG ``any_invocable`` October 2021
27 `P0798R8 <https://wg21.link/P0798R8>`__ LWG Monadic operations for ``std::optional`` October 2021 |Complete| 14.0
28 `P0849R8 <https://wg21.link/P0849R8>`__ LWG ``auto(x)``: ``DECAY_COPY`` in the language October 2021
29 `P1072R10 <https://wg21.link/P1072R10>`__ LWG ``basic_string::resize_and_overwrite`` October 2021
30 `P1147R1 <https://wg21.link/P1147R1>`__ LWG Printing ``volatile`` Pointers October 2021 |Complete| 14.0

View File

@ -132,6 +132,18 @@ namespace std {
template <class U> constexpr T value_or(U &&) const &;
template <class U> constexpr T value_or(U &&) &&;
// [optional.monadic], monadic operations
template<class F> constexpr auto and_then(F&& f) &; // since C++23
template<class F> constexpr auto and_then(F&& f) &&; // since C++23
template<class F> constexpr auto and_then(F&& f) const&; // since C++23
template<class F> constexpr auto and_then(F&& f) const&&; // since C++23
template<class F> constexpr auto transform(F&& f) &; // since C++23
template<class F> constexpr auto transform(F&& f) &&; // since C++23
template<class F> constexpr auto transform(F&& f) const&; // since C++23
template<class F> constexpr auto transform(F&& f) const&&; // since C++23
template<class F> constexpr optional or_else(F&& f) &&; // since C++23
template<class F> constexpr optional or_else(F&& f) const&; // since C++23
// 23.6.3.6, modifiers
void reset() noexcept; // constexpr in C++20
@ -147,6 +159,7 @@ template<class T>
*/
#include <__availability>
#include <__concepts/invocable.h>
#include <__config>
#include <__debug>
#include <__functional_base>
@ -200,6 +213,8 @@ struct nullopt_t
inline constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
struct __optional_construct_from_invoke_tag {};
template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
struct __optional_destruct_base;
@ -234,6 +249,13 @@ struct __optional_destruct_base<_Tp, false>
: __val_(_VSTD::forward<_Args>(__args)...),
__engaged_(true) {}
#if _LIBCPP_STD_VER > 20
template <class _Fp, class... _Args>
_LIBCPP_HIDE_FROM_ABI
constexpr __optional_destruct_base(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
: __val_(_VSTD::invoke(_VSTD::forward<_Fp>(__f), _VSTD::forward<_Args>(__args)...)), __engaged_(true) {}
#endif
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept
{
@ -269,6 +291,13 @@ struct __optional_destruct_base<_Tp, true>
: __val_(_VSTD::forward<_Args>(__args)...),
__engaged_(true) {}
#if _LIBCPP_STD_VER > 20
template <class _Fp, class... _Args>
_LIBCPP_HIDE_FROM_ABI
constexpr __optional_destruct_base(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
: __val_(_VSTD::invoke(_VSTD::forward<_Fp>(__f), _VSTD::forward<_Args>(__args)...)), __engaged_(true) {}
#endif
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR_AFTER_CXX17 void reset() noexcept
{
@ -582,6 +611,12 @@ using __optional_sfinae_assign_base_t = __sfinae_assign_base<
(is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value)
>;
template<class _Tp>
class optional;
template <class _Tp>
struct __is_std_optional : false_type {};
template <class _Tp> struct __is_std_optional<optional<_Tp>> : true_type {};
template <class _Tp>
class optional
: private __optional_move_assign_base<_Tp>
@ -684,6 +719,7 @@ private:
_CheckOptionalLikeConstructor<_QualUp>,
__check_tuple_constructor_fail
>;
public:
_LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
@ -759,6 +795,14 @@ public:
this->__construct_from(_VSTD::move(__v));
}
#if _LIBCPP_STD_VER > 20
template<class _Fp, class... _Args>
_LIBCPP_HIDE_FROM_ABI
constexpr explicit optional(__optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
: __base(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Fp>(__f), _VSTD::forward<_Args>(__args)...) {
}
#endif
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR_AFTER_CXX17 optional& operator=(nullopt_t) noexcept
{
@ -993,6 +1037,132 @@ public:
static_cast<value_type>(_VSTD::forward<_Up>(__v));
}
#if _LIBCPP_STD_VER > 20
template<class _Func>
_LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func&& __f) & {
using _Up = invoke_result_t<_Func, value_type&>;
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
"Result of f(value()) must be a specialization of std::optional");
if (*this)
return _VSTD::invoke(_VSTD::forward<_Func>(__f), value());
return remove_cvref_t<_Up>();
}
template<class _Func>
_LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func&& __f) const& {
using _Up = invoke_result_t<_Func, const value_type&>;
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
"Result of f(value()) must be a specialization of std::optional");
if (*this)
return _VSTD::invoke(_VSTD::forward<_Func>(__f), value());
return remove_cvref_t<_Up>();
}
template<class _Func>
_LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func&& __f) && {
using _Up = invoke_result_t<_Func, value_type&&>;
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
"Result of f(std::move(value())) must be a specialization of std::optional");
if (*this)
return _VSTD::invoke(_VSTD::forward<_Func>(__f), _VSTD::move(value()));
return remove_cvref_t<_Up>();
}
template<class _Func>
_LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func&& __f) const&& {
using _Up = invoke_result_t<_Func, const value_type&&>;
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
"Result of f(std::move(value())) must be a specialization of std::optional");
if (*this)
return _VSTD::invoke(_VSTD::forward<_Func>(__f), _VSTD::move(value()));
return remove_cvref_t<_Up>();
}
template<class _Func>
_LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func&& __f) & {
using _Up = remove_cv_t<invoke_result_t<_Func, value_type&>>;
static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
static_assert(!is_same_v<_Up, in_place_t>,
"Result of f(value()) should not be std::in_place_t");
static_assert(!is_same_v<_Up, nullopt_t>,
"Result of f(value()) should not be std::nullopt_t");
static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
if (*this)
return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), value());
return optional<_Up>();
}
template<class _Func>
_LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func&& __f) const& {
using _Up = remove_cv_t<invoke_result_t<_Func, const value_type&>>;
static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
static_assert(!is_same_v<_Up, in_place_t>,
"Result of f(value()) should not be std::in_place_t");
static_assert(!is_same_v<_Up, nullopt_t>,
"Result of f(value()) should not be std::nullopt_t");
static_assert(is_object_v<_Up>, "Result of f(value()) should be an object type");
if (*this)
return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), value());
return optional<_Up>();
}
template<class _Func>
_LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func&& __f) && {
using _Up = remove_cv_t<invoke_result_t<_Func, value_type&&>>;
static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
static_assert(!is_same_v<_Up, in_place_t>,
"Result of f(std::move(value())) should not be std::in_place_t");
static_assert(!is_same_v<_Up, nullopt_t>,
"Result of f(std::move(value())) should not be std::nullopt_t");
static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
if (*this)
return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), _VSTD::move(value()));
return optional<_Up>();
}
template<class _Func>
_LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func&& __f) const&& {
using _Up = remove_cvref_t<invoke_result_t<_Func, const value_type&&>>;
static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
static_assert(!is_same_v<_Up, in_place_t>,
"Result of f(std::move(value())) should not be std::in_place_t");
static_assert(!is_same_v<_Up, nullopt_t>,
"Result of f(std::move(value())) should not be std::nullopt_t");
static_assert(is_object_v<_Up>, "Result of f(std::move(value())) should be an object type");
if (*this)
return optional<_Up>(__optional_construct_from_invoke_tag{}, _VSTD::forward<_Func>(__f), _VSTD::move(value()));
return optional<_Up>();
}
template<invocable _Func>
_LIBCPP_HIDE_FROM_ABI
constexpr optional or_else(_Func&& __f) const& requires is_copy_constructible_v<value_type> {
static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
"Result of f() should be the same type as this optional");
if (*this)
return *this;
return _VSTD::forward<_Func>(__f)();
}
template<invocable _Func>
_LIBCPP_HIDE_FROM_ABI
constexpr optional or_else(_Func&& __f) && requires is_move_constructible_v<value_type> {
static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
"Result of f() should be the same type as this optional");
if (*this)
return _VSTD::move(*this);
return _VSTD::forward<_Func>(__f)();
}
#endif // _LIBCPP_STD_VER > 20
using __base::reset;
};

View File

@ -109,6 +109,7 @@ __cpp_lib_map_try_emplace 201411L <map>
__cpp_lib_math_constants 201907L <numbers>
__cpp_lib_math_special_functions 201603L <cmath>
__cpp_lib_memory_resource 201603L <memory_resource>
__cpp_lib_monadic_optional 202110L <optional>
__cpp_lib_node_extract 201606L <map> <set> <unordered_map>
<unordered_set>
__cpp_lib_nonmember_container_access 201411L <array> <deque> <forward_list>
@ -347,6 +348,7 @@ __cpp_lib_void_t 201411L <type_traits>
#if _LIBCPP_STD_VER > 20
# define __cpp_lib_byteswap 202110L
# define __cpp_lib_is_scoped_enum 202011L
# define __cpp_lib_monadic_optional 202110L
// # define __cpp_lib_stacktrace 202011L
// # define __cpp_lib_stdatomic_h 202011L
# define __cpp_lib_string_contains 202011L

View File

@ -15,8 +15,9 @@
// Test the feature test macros defined by <optional>
/* Constant Value
__cpp_lib_optional 201606L [C++17]
/* Constant Value
__cpp_lib_monadic_optional 202110L [C++2b]
__cpp_lib_optional 201606L [C++17]
*/
#include <optional>
@ -24,18 +25,30 @@
#if TEST_STD_VER < 14
# ifdef __cpp_lib_monadic_optional
# error "__cpp_lib_monadic_optional should not be defined before c++2b"
# endif
# ifdef __cpp_lib_optional
# error "__cpp_lib_optional should not be defined before c++17"
# endif
#elif TEST_STD_VER == 14
# ifdef __cpp_lib_monadic_optional
# error "__cpp_lib_monadic_optional should not be defined before c++2b"
# endif
# ifdef __cpp_lib_optional
# error "__cpp_lib_optional should not be defined before c++17"
# endif
#elif TEST_STD_VER == 17
# ifdef __cpp_lib_monadic_optional
# error "__cpp_lib_monadic_optional should not be defined before c++2b"
# endif
# ifndef __cpp_lib_optional
# error "__cpp_lib_optional should be defined in c++17"
# endif
@ -45,6 +58,10 @@
#elif TEST_STD_VER == 20
# ifdef __cpp_lib_monadic_optional
# error "__cpp_lib_monadic_optional should not be defined before c++2b"
# endif
# ifndef __cpp_lib_optional
# error "__cpp_lib_optional should be defined in c++20"
# endif
@ -54,6 +71,13 @@
#elif TEST_STD_VER > 20
# ifndef __cpp_lib_monadic_optional
# error "__cpp_lib_monadic_optional should be defined in c++2b"
# endif
# if __cpp_lib_monadic_optional != 202110L
# error "__cpp_lib_monadic_optional should have the value 202110L in c++2b"
# endif
# ifndef __cpp_lib_optional
# error "__cpp_lib_optional should be defined in c++2b"
# endif

View File

@ -104,6 +104,7 @@
__cpp_lib_math_constants 201907L [C++20]
__cpp_lib_math_special_functions 201603L [C++17]
__cpp_lib_memory_resource 201603L [C++17]
__cpp_lib_monadic_optional 202110L [C++2b]
__cpp_lib_node_extract 201606L [C++17]
__cpp_lib_nonmember_container_access 201411L [C++17]
__cpp_lib_not_fn 201603L [C++17]
@ -504,6 +505,10 @@
# error "__cpp_lib_memory_resource should not be defined before c++17"
# endif
# ifdef __cpp_lib_monadic_optional
# error "__cpp_lib_monadic_optional should not be defined before c++2b"
# endif
# ifdef __cpp_lib_node_extract
# error "__cpp_lib_node_extract should not be defined before c++17"
# endif
@ -1068,6 +1073,10 @@
# error "__cpp_lib_memory_resource should not be defined before c++17"
# endif
# ifdef __cpp_lib_monadic_optional
# error "__cpp_lib_monadic_optional should not be defined before c++2b"
# endif
# ifdef __cpp_lib_node_extract
# error "__cpp_lib_node_extract should not be defined before c++17"
# endif
@ -1794,6 +1803,10 @@
# endif
# endif
# ifdef __cpp_lib_monadic_optional
# error "__cpp_lib_monadic_optional should not be defined before c++2b"
# endif
# ifndef __cpp_lib_node_extract
# error "__cpp_lib_node_extract should be defined in c++17"
# endif
@ -2832,6 +2845,10 @@
# endif
# endif
# ifdef __cpp_lib_monadic_optional
# error "__cpp_lib_monadic_optional should not be defined before c++2b"
# endif
# ifndef __cpp_lib_node_extract
# error "__cpp_lib_node_extract should be defined in c++20"
# endif
@ -3963,6 +3980,13 @@
# endif
# endif
# ifndef __cpp_lib_monadic_optional
# error "__cpp_lib_monadic_optional should be defined in c++2b"
# endif
# if __cpp_lib_monadic_optional != 202110L
# error "__cpp_lib_monadic_optional should have the value 202110L in c++2b"
# endif
# ifndef __cpp_lib_node_extract
# error "__cpp_lib_node_extract should be defined in c++2b"
# endif

View File

@ -0,0 +1,262 @@
//===----------------------------------------------------------------------===//
//
// 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
// <optional>
// template<class F> constexpr auto and_then(F&&) &;
// template<class F> constexpr auto and_then(F&&) &&;
// template<class F> constexpr auto and_then(F&&) const&;
// template<class F> constexpr auto and_then(F&&) const&&;
#include <cassert>
#include <optional>
#include "test_macros.h"
struct LVal {
constexpr std::optional<int> operator()(int&) { return 1; }
std::optional<int> operator()(const int&) = delete;
std::optional<int> operator()(int&&) = delete;
std::optional<int> operator()(const int&&) = delete;
};
struct CLVal {
std::optional<int> operator()(int&) = delete;
constexpr std::optional<int> operator()(const int&) { return 1; }
std::optional<int> operator()(int&&) = delete;
std::optional<int> operator()(const int&&) = delete;
};
struct RVal {
std::optional<int> operator()(int&) = delete;
std::optional<int> operator()(const int&) = delete;
constexpr std::optional<int> operator()(int&&) { return 1; }
std::optional<int> operator()(const int&&) = delete;
};
struct CRVal {
std::optional<int> operator()(int&) = delete;
std::optional<int> operator()(const int&) = delete;
std::optional<int> operator()(int&&) = delete;
constexpr std::optional<int> operator()(const int&&) { return 1; }
};
struct RefQual {
constexpr std::optional<int> operator()(int) & { return 1; }
std::optional<int> operator()(int) const& = delete;
std::optional<int> operator()(int) && = delete;
std::optional<int> operator()(int) const&& = delete;
};
struct CRefQual {
std::optional<int> operator()(int) & = delete;
constexpr std::optional<int> operator()(int) const& { return 1; }
std::optional<int> operator()(int) && = delete;
std::optional<int> operator()(int) const&& = delete;
};
struct RVRefQual {
std::optional<int> operator()(int) & = delete;
std::optional<int> operator()(int) const& = delete;
constexpr std::optional<int> operator()(int) && { return 1; }
std::optional<int> operator()(int) const&& = delete;
};
struct RVCRefQual {
std::optional<int> operator()(int) & = delete;
std::optional<int> operator()(int) const& = delete;
std::optional<int> operator()(int) && = delete;
constexpr std::optional<int> operator()(int) const&& { return 1; }
};
struct NOLVal {
constexpr std::optional<int> operator()(int&) { return std::nullopt; }
std::optional<int> operator()(const int&) = delete;
std::optional<int> operator()(int&&) = delete;
std::optional<int> operator()(const int&&) = delete;
};
struct NOCLVal {
std::optional<int> operator()(int&) = delete;
constexpr std::optional<int> operator()(const int&) { return std::nullopt; }
std::optional<int> operator()(int&&) = delete;
std::optional<int> operator()(const int&&) = delete;
};
struct NORVal {
std::optional<int> operator()(int&) = delete;
std::optional<int> operator()(const int&) = delete;
constexpr std::optional<int> operator()(int&&) { return std::nullopt; }
std::optional<int> operator()(const int&&) = delete;
};
struct NOCRVal {
std::optional<int> operator()(int&) = delete;
std::optional<int> operator()(const int&) = delete;
std::optional<int> operator()(int&&) = delete;
constexpr std::optional<int> operator()(const int&&) { return std::nullopt; }
};
struct NORefQual {
constexpr std::optional<int> operator()(int) & { return std::nullopt; }
std::optional<int> operator()(int) const& = delete;
std::optional<int> operator()(int) && = delete;
std::optional<int> operator()(int) const&& = delete;
};
struct NOCRefQual {
std::optional<int> operator()(int) & = delete;
constexpr std::optional<int> operator()(int) const& { return std::nullopt; }
std::optional<int> operator()(int) && = delete;
std::optional<int> operator()(int) const&& = delete;
};
struct NORVRefQual {
std::optional<int> operator()(int) & = delete;
std::optional<int> operator()(int) const& = delete;
constexpr std::optional<int> operator()(int) && { return std::nullopt; }
std::optional<int> operator()(int) const&& = delete;
};
struct NORVCRefQual {
std::optional<int> operator()(int) & = delete;
std::optional<int> operator()(int) const& = delete;
std::optional<int> operator()(int) && = delete;
constexpr std::optional<int> operator()(int) const&& { return std::nullopt; }
};
struct NoCopy {
NoCopy() = default;
NoCopy(const NoCopy&) { assert(false); }
std::optional<int> operator()(const NoCopy&&) { return 1; }
};
struct NonConst {
std::optional<int> non_const() { return 1; }
};
constexpr void test_val_types() {
// Test & overload
{
// Without & qualifier on F's operator()
{
std::optional<int> i{0};
assert(i.and_then(LVal{}) == 1);
assert(i.and_then(NOLVal{}) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(LVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
std::optional<int> i{0};
RefQual l{};
assert(i.and_then(l) == 1);
NORefQual nl{};
assert(i.and_then(nl) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(l)), std::optional<int>);
}
}
// Test const& overload
{
// Without & qualifier on F's operator()
{
const std::optional<int> i{0};
assert(i.and_then(CLVal{}) == 1);
assert(i.and_then(NOCLVal{}) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(CLVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
const std::optional<int> i{0};
const CRefQual l{};
assert(i.and_then(l) == 1);
const NOCRefQual nl{};
assert(i.and_then(nl) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(l)), std::optional<int>);
}
}
// Test && overload
{
// Without & qualifier on F's operator()
{
std::optional<int> i{0};
assert(std::move(i).and_then(RVal{}) == 1);
assert(std::move(i).and_then(NORVal{}) == std::nullopt);
ASSERT_SAME_TYPE(decltype(std::move(i).and_then(RVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
std::optional<int> i{0};
assert(i.and_then(RVRefQual{}) == 1);
assert(i.and_then(NORVRefQual{}) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(RVRefQual{})), std::optional<int>);
}
}
// Test const&& overload
{
// Without & qualifier on F's operator()
{
const std::optional<int> i{0};
assert(std::move(i).and_then(CRVal{}) == 1);
assert(std::move(i).and_then(NOCRVal{}) == std::nullopt);
ASSERT_SAME_TYPE(decltype(std::move(i).and_then(CRVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
const std::optional<int> i{0};
const RVCRefQual l{};
assert(i.and_then(std::move(l)) == 1);
const NORVCRefQual nl{};
assert(i.and_then(std::move(nl)) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(std::move(l))), std::optional<int>);
}
}
}
// check that the lambda body is not instantiated during overload resolution
constexpr void test_sfinae() {
std::optional<NonConst> opt{};
auto l = [](auto&& x) { return x.non_const(); };
opt.and_then(l);
std::move(opt).and_then(l);
}
constexpr bool test() {
test_val_types();
std::optional<int> opt{};
const auto& copt = opt;
const auto never_called = [](int) {
assert(false);
return std::optional<int>{};
};
opt.and_then(never_called);
std::move(opt).and_then(never_called);
copt.and_then(never_called);
std::move(copt).and_then(never_called);
std::optional<NoCopy> nc;
const auto& cnc = nc;
std::move(cnc).and_then(NoCopy{});
std::move(nc).and_then(NoCopy{});
return true;
}
int main(int, char**) {
test();
static_assert(test());
}

View File

@ -0,0 +1,72 @@
//===----------------------------------------------------------------------===//
//
// 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
// <optional>
// template<class F> constexpr optional or_else(F&&) &&;
// template<class F> constexpr optional or_else(F&&) const&;
#include "MoveOnly.h"
#include <cassert>
#include <optional>
struct NonMovable {
NonMovable() = default;
NonMovable(NonMovable&&) = delete;
};
template <class Opt, class F>
concept has_or_else = requires(Opt&& opt, F&& f) {
{std::forward<Opt>(opt).or_else(std::forward<F>(f))};
};
template <class T>
std::optional<T> return_optional() {}
static_assert(has_or_else<std::optional<int>&, decltype(return_optional<int>)>);
static_assert(has_or_else<std::optional<int>&&, decltype(return_optional<int>)>);
static_assert(!has_or_else<std::optional<MoveOnly>&, decltype(return_optional<MoveOnly>)>);
static_assert(has_or_else<std::optional<MoveOnly>&&, decltype(return_optional<MoveOnly>)>);
static_assert(!has_or_else<std::optional<NonMovable>&, decltype(return_optional<NonMovable>)>);
static_assert(!has_or_else<std::optional<NonMovable>&&, decltype(return_optional<NonMovable>)>);
std::optional<int> take_int(int) { return 0; }
void take_int_return_void(int) {}
static_assert(!has_or_else<std::optional<int>, decltype(take_int)>);
static_assert(!has_or_else<std::optional<int>, decltype(take_int_return_void)>);
static_assert(!has_or_else<std::optional<int>, int>);
constexpr bool test() {
{
std::optional<int> opt;
assert(opt.or_else([] { return std::optional<int>{0}; }) == 0);
opt = 1;
opt.or_else([] {
assert(false);
return std::optional<int>{};
});
}
{
std::optional<MoveOnly> opt;
opt = std::move(opt).or_else([] { return std::optional<MoveOnly>{MoveOnly{}}; });
std::move(opt).or_else([] {
assert(false);
return std::optional<MoveOnly>{};
});
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
}

View File

@ -0,0 +1,205 @@
//===----------------------------------------------------------------------===//
//
// 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
// <optional>
// template<class F> constexpr auto transform(F&&) &;
// template<class F> constexpr auto transform(F&&) &&;
// template<class F> constexpr auto transform(F&&) const&;
// template<class F> constexpr auto transform(F&&) const&&;
#include "test_macros.h"
#include <cassert>
#include <optional>
#include <type_traits>
struct LVal {
constexpr int operator()(int&) { return 1; }
int operator()(const int&) = delete;
int operator()(int&&) = delete;
int operator()(const int&&) = delete;
};
struct CLVal {
int operator()(int&) = delete;
constexpr int operator()(const int&) { return 1; }
int operator()(int&&) = delete;
int operator()(const int&&) = delete;
};
struct RVal {
int operator()(int&) = delete;
int operator()(const int&) = delete;
constexpr int operator()(int&&) { return 1; }
int operator()(const int&&) = delete;
};
struct CRVal {
int operator()(int&) = delete;
int operator()(const int&) = delete;
int operator()(int&&) = delete;
constexpr int operator()(const int&&) { return 1; }
};
struct RefQual {
constexpr int operator()(int) & { return 1; }
int operator()(int) const& = delete;
int operator()(int) && = delete;
int operator()(int) const&& = delete;
};
struct CRefQual {
int operator()(int) & = delete;
constexpr int operator()(int) const& { return 1; }
int operator()(int) && = delete;
int operator()(int) const&& = delete;
};
struct RVRefQual {
int operator()(int) & = delete;
int operator()(int) const& = delete;
constexpr int operator()(int) && { return 1; }
int operator()(int) const&& = delete;
};
struct RVCRefQual {
int operator()(int) & = delete;
int operator()(int) const& = delete;
int operator()(int) && = delete;
constexpr int operator()(int) const&& { return 1; }
};
struct NoCopy {
NoCopy() = default;
NoCopy(const NoCopy&) { assert(false); }
int operator()(const NoCopy&&) { return 1; }
};
struct NoMove {
NoMove() = default;
NoMove(NoMove&&) = delete;
NoMove operator()(const NoCopy&&) { return NoMove{}; }
};
constexpr void test_val_types() {
// Test & overload
{
// Without & qualifier on F's operator()
{
std::optional<int> i{0};
assert(i.transform(LVal{}) == 1);
ASSERT_SAME_TYPE(decltype(i.transform(LVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
std::optional<int> i{0};
RefQual l{};
assert(i.transform(l) == 1);
ASSERT_SAME_TYPE(decltype(i.transform(l)), std::optional<int>);
}
}
// Test const& overload
{
// Without & qualifier on F's operator()
{
const std::optional<int> i{0};
assert(i.transform(CLVal{}) == 1);
ASSERT_SAME_TYPE(decltype(i.transform(CLVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
const std::optional<int> i{0};
const CRefQual l{};
assert(i.transform(l) == 1);
ASSERT_SAME_TYPE(decltype(i.transform(l)), std::optional<int>);
}
}
// Test && overload
{
// Without & qualifier on F's operator()
{
std::optional<int> i{0};
assert(std::move(i).transform(RVal{}) == 1);
ASSERT_SAME_TYPE(decltype(std::move(i).transform(RVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
std::optional<int> i{0};
assert(i.transform(RVRefQual{}) == 1);
ASSERT_SAME_TYPE(decltype(i.transform(RVRefQual{})), std::optional<int>);
}
}
// Test const&& overload
{
// Without & qualifier on F's operator()
{
const std::optional<int> i{0};
assert(std::move(i).transform(CRVal{}) == 1);
ASSERT_SAME_TYPE(decltype(std::move(i).transform(CRVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
const std::optional<int> i{0};
const RVCRefQual l{};
assert(i.transform(std::move(l)) == 1);
ASSERT_SAME_TYPE(decltype(i.transform(std::move(l))), std::optional<int>);
}
}
}
struct NonConst {
int non_const() { return 1; }
};
// check that the lambda body is not instantiated during overload resolution
constexpr void test_sfinae() {
std::optional<NonConst> opt{};
auto l = [](auto&& x) { return x.non_const(); };
opt.transform(l);
std::move(opt).transform(l);
}
constexpr bool test() {
test_sfinae();
test_val_types();
std::optional<int> opt;
const auto& copt = opt;
const auto never_called = [](int) {
assert(false);
return 0;
};
opt.transform(never_called);
std::move(opt).transform(never_called);
copt.transform(never_called);
std::move(copt).transform(never_called);
std::optional<NoCopy> nc;
const auto& cnc = nc;
std::move(nc).transform(NoCopy{});
std::move(cnc).transform(NoCopy{});
std::move(nc).transform(NoMove{});
std::move(cnc).transform(NoMove{});
return true;
}
int main(int, char**) {
test();
static_assert(test());
}

View File

@ -442,6 +442,10 @@ feature_test_macros = [ add_version_header(x) for x in [
"values": { "c++17": 201603 },
"headers": ["memory_resource"],
"unimplemented": True,
}, {
"name": "__cpp_lib_monadic_optional",
"values": { "c++2b": 202110 },
"headers": ["optional"],
}, {
"name": "__cpp_lib_node_extract",
"values": { "c++17": 201606 },