summaryrefslogtreecommitdiff
path: root/include/variant
diff options
context:
space:
mode:
Diffstat (limited to 'include/variant')
-rw-r--r--include/variant62
1 files changed, 35 insertions, 27 deletions
diff --git a/include/variant b/include/variant
index bbd4bf4895ad3..88f7b240d029d 100644
--- a/include/variant
+++ b/include/variant
@@ -53,16 +53,16 @@ namespace std {
// 20.7.2.4, modifiers
template <class T, class... Args>
- void emplace(Args&&...);
+ T& emplace(Args&&...);
template <class T, class U, class... Args>
- void emplace(initializer_list<U>, Args&&...);
+ T& emplace(initializer_list<U>, Args&&...);
template <size_t I, class... Args>
- void emplace(Args&&...);
+ variant_alternative_t<I, variant>& emplace(Args&&...);
template <size_t I, class U, class... Args>
- void emplace(initializer_list<U>, Args&&...);
+ variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
// 20.7.2.5, value status
constexpr bool valueless_by_exception() const noexcept;
@@ -466,17 +466,21 @@ private:
return __result{{_VSTD::forward<_Fs>(__fs)...}};
}
- template <class _Fp, class... _Vs, size_t... _Is>
- inline _LIBCPP_INLINE_VISIBILITY
- static constexpr auto __make_dispatch(index_sequence<_Is...>) {
- struct __dispatcher {
- static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
+ template <std::size_t... _Is>
+ struct __dispatcher {
+ template <class _Fp, class... _Vs>
+ inline _LIBCPP_INLINE_VISIBILITY
+ static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
return __invoke_constexpr(
static_cast<_Fp>(__f),
__access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
- }
- };
- return _VSTD::addressof(__dispatcher::__dispatch);
+ }
+ };
+
+ template <class _Fp, class... _Vs, size_t... _Is>
+ inline _LIBCPP_INLINE_VISIBILITY
+ static constexpr auto __make_dispatch(index_sequence<_Is...>) {
+ return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
}
template <size_t _Ip, class _Fp, class... _Vs>
@@ -760,9 +764,10 @@ public:
protected:
template <size_t _Ip, class _Tp, class... _Args>
inline _LIBCPP_INLINE_VISIBILITY
- static void __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
- ::new (_VSTD::addressof(__a))
+ static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
+ ::new ((void*)_VSTD::addressof(__a))
__alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
+ return __a.__value;
}
template <class _Rhs>
@@ -872,11 +877,12 @@ public:
template <size_t _Ip, class... _Args>
inline _LIBCPP_INLINE_VISIBILITY
- void __emplace(_Args&&... __args) {
+ auto& __emplace(_Args&&... __args) {
this->__destroy();
- this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
+ auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
_VSTD::forward<_Args>(__args)...);
this->__index = _Ip;
+ return __res;
}
protected:
@@ -1214,8 +1220,8 @@ public:
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
- void emplace(_Args&&... __args) {
- __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
+ _Tp& emplace(_Args&&... __args) {
+ return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
}
template <
@@ -1227,8 +1233,8 @@ public:
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
- void emplace(initializer_list<_Up> __il, _Args&&... __args) {
- __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
+ _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
+ return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
}
template <
@@ -1238,8 +1244,8 @@ public:
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
- void emplace(_Args&&... __args) {
- __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
+ _Tp& emplace(_Args&&... __args) {
+ return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
}
template <
@@ -1251,8 +1257,8 @@ public:
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
- void emplace(initializer_list<_Up> __il, _Args&&... __args) {
- __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
+ _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
+ return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
}
inline _LIBCPP_INLINE_VISIBILITY
@@ -1529,7 +1535,8 @@ auto swap(variant<_Types...>& __lhs,
}
template <class... _Types>
-struct _LIBCPP_TEMPLATE_VIS hash<variant<_Types...>> {
+struct _LIBCPP_TEMPLATE_VIS hash<
+ __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
using argument_type = variant<_Types...>;
using result_type = size_t;
@@ -1542,7 +1549,8 @@ struct _LIBCPP_TEMPLATE_VIS hash<variant<_Types...>> {
: __variant::__visit_alt(
[](const auto& __alt) {
using __alt_type = decay_t<decltype(__alt)>;
- using __value_type = typename __alt_type::__value_type;
+ using __value_type = remove_const_t<
+ typename __alt_type::__value_type>;
return hash<__value_type>{}(__alt.__value);
},
__v);
@@ -1556,7 +1564,7 @@ struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
using result_type = size_t;
inline _LIBCPP_INLINE_VISIBILITY
- result_type operator()(const argument_type&) const {
+ result_type operator()(const argument_type&) const _NOEXCEPT {
return 66740831; // return a fundamentally attractive random value.
}
};