2016-08-18 22:31:26 +02:00
|
|
|
// <variant> -*- C++ -*-
|
|
|
|
|
2017-01-01 13:07:43 +01:00
|
|
|
// Copyright (C) 2016-2017 Free Software Foundation, Inc.
|
2016-08-18 22:31:26 +02:00
|
|
|
//
|
|
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
|
// software; you can redistribute it and/or modify it under the
|
|
|
|
// terms of the GNU General Public License as published by the
|
|
|
|
// Free Software Foundation; either version 3, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
// permissions described in the GCC Runtime Library Exception, version
|
|
|
|
// 3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License and
|
|
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/** @file variant
|
|
|
|
* This is the <variant> C++ Library header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _GLIBCXX_VARIANT
|
|
|
|
#define _GLIBCXX_VARIANT 1
|
|
|
|
|
|
|
|
#pragma GCC system_header
|
|
|
|
|
|
|
|
#if __cplusplus <= 201402L
|
|
|
|
# include <bits/c++17_warning.h>
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
#include <bits/enable_special_members.h>
|
2016-10-14 11:58:05 +02:00
|
|
|
#include <bits/functexcept.h>
|
2016-09-05 21:52:11 +02:00
|
|
|
#include <bits/move.h>
|
2016-11-14 22:22:53 +01:00
|
|
|
#include <bits/functional_hash.h>
|
2016-12-06 12:28:09 +01:00
|
|
|
#include <bits/invoke.h>
|
2016-12-06 12:20:13 +01:00
|
|
|
#include <ext/aligned_buffer.h>
|
2017-01-11 12:23:43 +01:00
|
|
|
#include <bits/parse_numbers.h>
|
2017-06-05 18:49:04 +02:00
|
|
|
#include <bits/stl_iterator_base_types.h>
|
|
|
|
#include <bits/stl_iterator_base_funcs.h>
|
|
|
|
#include <bits/stl_construct.h>
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
namespace std _GLIBCXX_VISIBILITY(default)
|
|
|
|
{
|
2016-12-06 12:28:09 +01:00
|
|
|
namespace __detail
|
|
|
|
{
|
|
|
|
namespace __variant
|
|
|
|
{
|
|
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Types>
|
|
|
|
struct _Nth_type;
|
|
|
|
|
|
|
|
template<size_t _Np, typename _First, typename... _Rest>
|
|
|
|
struct _Nth_type<_Np, _First, _Rest...>
|
|
|
|
: _Nth_type<_Np-1, _Rest...> { };
|
|
|
|
|
|
|
|
template<typename _First, typename... _Rest>
|
|
|
|
struct _Nth_type<0, _First, _Rest...>
|
|
|
|
{ using type = _First; };
|
|
|
|
|
|
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
|
|
|
} // namespace __variant
|
|
|
|
} // namespace __detail
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
|
|
|
|
|
template<typename... _Types> class tuple;
|
|
|
|
template<typename... _Types> class variant;
|
|
|
|
template <typename> struct hash;
|
|
|
|
|
|
|
|
template<typename _Variant>
|
|
|
|
struct variant_size;
|
|
|
|
|
|
|
|
template<typename _Variant>
|
|
|
|
struct variant_size<const _Variant> : variant_size<_Variant> {};
|
|
|
|
|
|
|
|
template<typename _Variant>
|
|
|
|
struct variant_size<volatile _Variant> : variant_size<_Variant> {};
|
|
|
|
|
|
|
|
template<typename _Variant>
|
|
|
|
struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
|
|
|
|
|
|
|
|
template<typename... _Types>
|
|
|
|
struct variant_size<variant<_Types...>>
|
|
|
|
: std::integral_constant<size_t, sizeof...(_Types)> {};
|
|
|
|
|
|
|
|
template<typename _Variant>
|
Implement P0607R0 "Inline Variables for Standard Library" for C++17
2017-03-23 Daniel Kruegler <daniel.kruegler@gmail.com>
* include/bits/c++config (_GLIBCXX17_INLINE): Define.
* include/bits/regex_constants.h (All std::regex_constants constants):
Add _GLIBCXX17_INLINE as per P0607R0.
* include/bits/std_mutex.h (defer_lock, try_to_lock, adopt_lock):
Likewise.
* include/bits/stl_pair.h (piecewise_construct): Likewise.
* include/bits/uses_allocator.h (allocator_arg, uses_allocator_v)
(__is_uses_allocator_constructible_v)
(__is_nothrow_uses_allocator_constructible_v): Likewise.
* include/std/chrono (treat_as_floating_point_v): Likewise.
* include/std/functional (is_bind_expression_v, is_placeholder_v):
Likewise.
* include/std/optional (nullopt): Likewise.
* include/std/ratio (ratio_equal_v, ratio_not_equal_v, ratio_less_v)
ratio_less_equal_v, ratio_greater_v, ratio_greater_equal_v): Likewise.
* include/std/system_error (is_error_code_enum_v)
(is_error_condition_enum_v): Likewise.
* include/std/tuple (tuple_size_v, ignore): Likewise.
(ignore): Declare ignore constexpr as per LWG 2773, declare assignment
constexpr as per LWG 2933.
* include/std/type_traits (All variable templates): Add
_GLIBCXX17_INLINE as per P0607R0.
* include/std/variant (variant_size_v, variant_npos, __index_of_v)
(__tuple_count_v, __exactly_once): Likewise.
* testsuite/18_support/headers/new/synopsis.cc
(hardware_destructive_interference_size)
(hardware_constructive_interference_size): Likewise for commented-out
variables.
* testsuite/20_util/tuple/creation_functions/constexpr.cc: Add new
test function for constexpr std::ignore (LWG 2773).
* testsuite/20_util/tuple/creation_functions/constexpr_cpp14.cc: New
test for LWG 2933.
From-SVN: r246423
2017-03-23 20:40:07 +01:00
|
|
|
inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<size_t _Np, typename _Variant>
|
|
|
|
struct variant_alternative;
|
|
|
|
|
|
|
|
template<size_t _Np, typename _First, typename... _Rest>
|
|
|
|
struct variant_alternative<_Np, variant<_First, _Rest...>>
|
|
|
|
: variant_alternative<_Np-1, variant<_Rest...>> {};
|
|
|
|
|
|
|
|
template<typename _First, typename... _Rest>
|
|
|
|
struct variant_alternative<0, variant<_First, _Rest...>>
|
|
|
|
{ using type = _First; };
|
|
|
|
|
|
|
|
template<size_t _Np, typename _Variant>
|
|
|
|
using variant_alternative_t =
|
|
|
|
typename variant_alternative<_Np, _Variant>::type;
|
|
|
|
|
2016-11-27 01:32:04 +01:00
|
|
|
template<size_t _Np, typename _Variant>
|
|
|
|
struct variant_alternative<_Np, const _Variant>
|
|
|
|
{ using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
|
|
|
|
|
|
|
|
template<size_t _Np, typename _Variant>
|
|
|
|
struct variant_alternative<_Np, volatile _Variant>
|
|
|
|
{ using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
|
|
|
|
|
|
|
|
template<size_t _Np, typename _Variant>
|
|
|
|
struct variant_alternative<_Np, const volatile _Variant>
|
|
|
|
{ using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
|
|
|
|
|
Implement P0607R0 "Inline Variables for Standard Library" for C++17
2017-03-23 Daniel Kruegler <daniel.kruegler@gmail.com>
* include/bits/c++config (_GLIBCXX17_INLINE): Define.
* include/bits/regex_constants.h (All std::regex_constants constants):
Add _GLIBCXX17_INLINE as per P0607R0.
* include/bits/std_mutex.h (defer_lock, try_to_lock, adopt_lock):
Likewise.
* include/bits/stl_pair.h (piecewise_construct): Likewise.
* include/bits/uses_allocator.h (allocator_arg, uses_allocator_v)
(__is_uses_allocator_constructible_v)
(__is_nothrow_uses_allocator_constructible_v): Likewise.
* include/std/chrono (treat_as_floating_point_v): Likewise.
* include/std/functional (is_bind_expression_v, is_placeholder_v):
Likewise.
* include/std/optional (nullopt): Likewise.
* include/std/ratio (ratio_equal_v, ratio_not_equal_v, ratio_less_v)
ratio_less_equal_v, ratio_greater_v, ratio_greater_equal_v): Likewise.
* include/std/system_error (is_error_code_enum_v)
(is_error_condition_enum_v): Likewise.
* include/std/tuple (tuple_size_v, ignore): Likewise.
(ignore): Declare ignore constexpr as per LWG 2773, declare assignment
constexpr as per LWG 2933.
* include/std/type_traits (All variable templates): Add
_GLIBCXX17_INLINE as per P0607R0.
* include/std/variant (variant_size_v, variant_npos, __index_of_v)
(__tuple_count_v, __exactly_once): Likewise.
* testsuite/18_support/headers/new/synopsis.cc
(hardware_destructive_interference_size)
(hardware_constructive_interference_size): Likewise for commented-out
variables.
* testsuite/20_util/tuple/creation_functions/constexpr.cc: Add new
test function for constexpr std::ignore (LWG 2773).
* testsuite/20_util/tuple/creation_functions/constexpr_cpp14.cc: New
test for LWG 2933.
From-SVN: r246423
2017-03-23 20:40:07 +01:00
|
|
|
inline constexpr size_t variant_npos = -1;
|
2016-08-18 22:31:26 +02:00
|
|
|
|
2016-12-06 12:28:09 +01:00
|
|
|
template<size_t _Np, typename... _Types>
|
|
|
|
constexpr variant_alternative_t<_Np, variant<_Types...>>&
|
|
|
|
get(variant<_Types...>&);
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Types>
|
|
|
|
constexpr variant_alternative_t<_Np, variant<_Types...>>&&
|
|
|
|
get(variant<_Types...>&&);
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Types>
|
|
|
|
constexpr variant_alternative_t<_Np, variant<_Types...>> const&
|
|
|
|
get(const variant<_Types...>&);
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Types>
|
|
|
|
constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
|
|
|
|
get(const variant<_Types...>&&);
|
|
|
|
|
2016-10-03 16:35:28 +02:00
|
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
namespace __detail
|
|
|
|
{
|
|
|
|
namespace __variant
|
|
|
|
{
|
2016-10-03 16:35:28 +02:00
|
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
2016-08-18 22:31:26 +02:00
|
|
|
// Returns the first apparence of _Tp in _Types.
|
|
|
|
// Returns sizeof...(_Types) if _Tp is not in _Types.
|
|
|
|
template<typename _Tp, typename... _Types>
|
|
|
|
struct __index_of : std::integral_constant<size_t, 0> {};
|
|
|
|
|
|
|
|
template<typename _Tp, typename... _Types>
|
Implement P0607R0 "Inline Variables for Standard Library" for C++17
2017-03-23 Daniel Kruegler <daniel.kruegler@gmail.com>
* include/bits/c++config (_GLIBCXX17_INLINE): Define.
* include/bits/regex_constants.h (All std::regex_constants constants):
Add _GLIBCXX17_INLINE as per P0607R0.
* include/bits/std_mutex.h (defer_lock, try_to_lock, adopt_lock):
Likewise.
* include/bits/stl_pair.h (piecewise_construct): Likewise.
* include/bits/uses_allocator.h (allocator_arg, uses_allocator_v)
(__is_uses_allocator_constructible_v)
(__is_nothrow_uses_allocator_constructible_v): Likewise.
* include/std/chrono (treat_as_floating_point_v): Likewise.
* include/std/functional (is_bind_expression_v, is_placeholder_v):
Likewise.
* include/std/optional (nullopt): Likewise.
* include/std/ratio (ratio_equal_v, ratio_not_equal_v, ratio_less_v)
ratio_less_equal_v, ratio_greater_v, ratio_greater_equal_v): Likewise.
* include/std/system_error (is_error_code_enum_v)
(is_error_condition_enum_v): Likewise.
* include/std/tuple (tuple_size_v, ignore): Likewise.
(ignore): Declare ignore constexpr as per LWG 2773, declare assignment
constexpr as per LWG 2933.
* include/std/type_traits (All variable templates): Add
_GLIBCXX17_INLINE as per P0607R0.
* include/std/variant (variant_size_v, variant_npos, __index_of_v)
(__tuple_count_v, __exactly_once): Likewise.
* testsuite/18_support/headers/new/synopsis.cc
(hardware_destructive_interference_size)
(hardware_constructive_interference_size): Likewise for commented-out
variables.
* testsuite/20_util/tuple/creation_functions/constexpr.cc: Add new
test function for constexpr std::ignore (LWG 2773).
* testsuite/20_util/tuple/creation_functions/constexpr_cpp14.cc: New
test for LWG 2933.
From-SVN: r246423
2017-03-23 20:40:07 +01:00
|
|
|
inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value;
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<typename _Tp, typename _First, typename... _Rest>
|
|
|
|
struct __index_of<_Tp, _First, _Rest...> :
|
|
|
|
std::integral_constant<size_t, is_same_v<_Tp, _First>
|
|
|
|
? 0 : __index_of_v<_Tp, _Rest...> + 1> {};
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
// _Uninitialized<T> is guaranteed to be a literal type, even if T is not.
|
|
|
|
// We have to do this, because [basic.types]p10.5.3 (n4606) is not implemented
|
|
|
|
// yet. When it's implemented, _Uninitialized<T> can be changed to the alias
|
|
|
|
// to T, therefore equivalent to being removed entirely.
|
|
|
|
//
|
|
|
|
// Another reason we may not want to remove _Uninitialzied<T> may be that, we
|
|
|
|
// want _Uninitialized<T> to be trivially destructible, no matter whether T
|
|
|
|
// is; but we will see.
|
|
|
|
template<typename _Type, bool = std::is_literal_type_v<_Type>>
|
2016-08-18 22:31:26 +02:00
|
|
|
struct _Uninitialized;
|
|
|
|
|
|
|
|
template<typename _Type>
|
|
|
|
struct _Uninitialized<_Type, true>
|
|
|
|
{
|
|
|
|
template<typename... _Args>
|
|
|
|
constexpr _Uninitialized(in_place_index_t<0>, _Args&&... __args)
|
|
|
|
: _M_storage(std::forward<_Args>(__args)...)
|
|
|
|
{ }
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr const _Type& _M_get() const &
|
|
|
|
{ return _M_storage; }
|
|
|
|
|
|
|
|
constexpr _Type& _M_get() &
|
|
|
|
{ return _M_storage; }
|
|
|
|
|
|
|
|
constexpr const _Type&& _M_get() const &&
|
|
|
|
{ return std::move(_M_storage); }
|
|
|
|
|
|
|
|
constexpr _Type&& _M_get() &&
|
|
|
|
{ return std::move(_M_storage); }
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
_Type _M_storage;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename _Type>
|
|
|
|
struct _Uninitialized<_Type, false>
|
|
|
|
{
|
|
|
|
template<typename... _Args>
|
|
|
|
constexpr _Uninitialized(in_place_index_t<0>, _Args&&... __args)
|
|
|
|
{ ::new (&_M_storage) _Type(std::forward<_Args>(__args)...); }
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
const _Type& _M_get() const &
|
|
|
|
{ return *_M_storage._M_ptr(); }
|
|
|
|
|
|
|
|
_Type& _M_get() &
|
|
|
|
{ return *_M_storage._M_ptr(); }
|
|
|
|
|
|
|
|
const _Type&& _M_get() const &&
|
|
|
|
{ return std::move(*_M_storage._M_ptr()); }
|
|
|
|
|
|
|
|
_Type&& _M_get() &&
|
|
|
|
{ return std::move(*_M_storage._M_ptr()); }
|
|
|
|
|
|
|
|
__gnu_cxx::__aligned_membuf<_Type> _M_storage;
|
2016-08-18 22:31:26 +02:00
|
|
|
};
|
|
|
|
|
2016-12-06 12:28:09 +01:00
|
|
|
template<typename _Ref>
|
|
|
|
_Ref __ref_cast(void* __ptr)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
2016-12-06 12:28:09 +01:00
|
|
|
return static_cast<_Ref>(*static_cast<remove_reference_t<_Ref>*>(__ptr));
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
template<typename _Union>
|
|
|
|
constexpr decltype(auto) __get(in_place_index_t<0>, _Union&& __u)
|
|
|
|
{ return std::forward<_Union>(__u)._M_first._M_get(); }
|
|
|
|
|
|
|
|
template<size_t _Np, typename _Union>
|
|
|
|
constexpr decltype(auto) __get(in_place_index_t<_Np>, _Union&& __u)
|
|
|
|
{ return __get(in_place_index<_Np-1>, std::forward<_Union>(__u)._M_rest); }
|
|
|
|
|
|
|
|
// Returns the typed storage for __v.
|
|
|
|
template<size_t _Np, typename _Variant>
|
|
|
|
constexpr decltype(auto) __get(_Variant&& __v)
|
|
|
|
{
|
|
|
|
return __get(std::in_place_index<_Np>, std::forward<_Variant>(__v)._M_u);
|
|
|
|
}
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
// Various functions as "vtable" entries, where those vtables are used by
|
|
|
|
// polymorphic operations.
|
|
|
|
template<typename _Lhs, typename _Rhs>
|
2017-06-05 18:49:04 +02:00
|
|
|
void
|
2016-08-18 22:31:26 +02:00
|
|
|
__erased_ctor(void* __lhs, void* __rhs)
|
2017-06-05 18:49:04 +02:00
|
|
|
{
|
|
|
|
using _Type = remove_reference_t<_Lhs>;
|
|
|
|
::new (__lhs) _Type(__variant::__ref_cast<_Rhs>(__rhs));
|
|
|
|
}
|
2016-08-18 22:31:26 +02:00
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
template<typename _Variant, size_t _Np>
|
2017-06-05 18:49:04 +02:00
|
|
|
void
|
2016-12-06 12:20:13 +01:00
|
|
|
__erased_dtor(_Variant&& __v)
|
2017-06-05 18:49:04 +02:00
|
|
|
{ std::_Destroy(std::__addressof(__get<_Np>(__v))); }
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<typename _Lhs, typename _Rhs>
|
2017-06-05 18:49:04 +02:00
|
|
|
void
|
2016-08-18 22:31:26 +02:00
|
|
|
__erased_assign(void* __lhs, void* __rhs)
|
2017-06-05 18:49:04 +02:00
|
|
|
{
|
|
|
|
__variant::__ref_cast<_Lhs>(__lhs) = __variant::__ref_cast<_Rhs>(__rhs);
|
|
|
|
}
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<typename _Lhs, typename _Rhs>
|
2017-06-05 18:49:04 +02:00
|
|
|
void
|
2016-08-18 22:31:26 +02:00
|
|
|
__erased_swap(void* __lhs, void* __rhs)
|
|
|
|
{
|
|
|
|
using std::swap;
|
2017-06-05 18:49:04 +02:00
|
|
|
swap(__variant::__ref_cast<_Lhs>(__lhs),
|
|
|
|
__variant::__ref_cast<_Rhs>(__rhs));
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
2017-02-15 10:01:06 +01:00
|
|
|
#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
|
|
|
|
template<typename _Variant, size_t _Np> \
|
|
|
|
constexpr bool \
|
|
|
|
__erased_##__NAME(const _Variant& __lhs, const _Variant& __rhs) \
|
|
|
|
{ \
|
|
|
|
return __get<_Np>(std::forward<_Variant>(__lhs)) \
|
|
|
|
__OP __get<_Np>(std::forward<_Variant>(__rhs)); \
|
2016-12-06 12:20:13 +01:00
|
|
|
}
|
2016-08-18 22:31:26 +02:00
|
|
|
|
2017-02-15 10:01:06 +01:00
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
|
|
|
|
|
|
|
|
#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<typename _Tp>
|
2017-06-05 18:49:04 +02:00
|
|
|
size_t
|
2016-08-18 22:31:26 +02:00
|
|
|
__erased_hash(void* __t)
|
2016-12-06 12:28:09 +01:00
|
|
|
{
|
|
|
|
return std::hash<remove_cv_t<remove_reference_t<_Tp>>>{}(
|
2017-06-05 18:49:04 +02:00
|
|
|
__variant::__ref_cast<_Tp>(__t));
|
2016-12-06 12:28:09 +01:00
|
|
|
}
|
2016-08-18 22:31:26 +02:00
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
// Defines members and ctors.
|
2016-08-18 22:31:26 +02:00
|
|
|
template<typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
union _Variadic_union { };
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<typename _First, typename... _Rest>
|
2016-12-06 12:20:13 +01:00
|
|
|
union _Variadic_union<_First, _Rest...>
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr _Variadic_union() : _M_rest() { }
|
|
|
|
|
|
|
|
template<typename... _Args>
|
|
|
|
constexpr _Variadic_union(in_place_index_t<0>, _Args&&... __args)
|
|
|
|
: _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Args>
|
|
|
|
constexpr _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
|
|
|
|
: _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
_Uninitialized<_First> _M_first;
|
|
|
|
_Variadic_union<_Rest...> _M_rest;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Defines index and the dtor, possibly trivial.
|
|
|
|
template<bool __trivially_destructible, typename... _Types>
|
|
|
|
struct _Variant_storage;
|
|
|
|
|
2017-01-11 12:23:43 +01:00
|
|
|
template <typename... _Types>
|
|
|
|
using __select_index =
|
|
|
|
typename __select_int::_Select_int_base<sizeof...(_Types)+1,
|
|
|
|
unsigned char,
|
|
|
|
unsigned short>
|
|
|
|
::type::value_type;
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
template<typename... _Types>
|
|
|
|
struct _Variant_storage<false, _Types...>
|
|
|
|
{
|
|
|
|
template<size_t... __indices>
|
|
|
|
static constexpr void (*_S_vtable[])(const _Variant_storage&) =
|
|
|
|
{ &__erased_dtor<const _Variant_storage&, __indices>... };
|
|
|
|
|
|
|
|
constexpr _Variant_storage() : _M_index(variant_npos) { }
|
2016-08-18 22:31:26 +02:00
|
|
|
|
2016-09-22 05:15:58 +02:00
|
|
|
template<size_t _Np, typename... _Args>
|
2016-08-18 22:31:26 +02:00
|
|
|
constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
|
2016-12-06 12:20:13 +01:00
|
|
|
: _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
|
|
|
|
_M_index(_Np)
|
2016-08-18 22:31:26 +02:00
|
|
|
{ }
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
template<size_t... __indices>
|
2016-12-06 12:26:48 +01:00
|
|
|
constexpr void _M_reset_impl(std::index_sequence<__indices...>)
|
2016-12-06 12:20:13 +01:00
|
|
|
{
|
2017-01-11 12:23:43 +01:00
|
|
|
if (_M_index != __index_type(variant_npos))
|
2016-12-06 12:20:13 +01:00
|
|
|
_S_vtable<__indices...>[_M_index](*this);
|
|
|
|
}
|
2016-08-18 22:31:26 +02:00
|
|
|
|
2016-12-06 12:26:48 +01:00
|
|
|
void _M_reset()
|
|
|
|
{
|
|
|
|
_M_reset_impl(std::index_sequence_for<_Types...>{});
|
|
|
|
_M_index = variant_npos;
|
|
|
|
}
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
~_Variant_storage()
|
2016-12-06 12:26:48 +01:00
|
|
|
{ _M_reset(); }
|
2016-08-18 22:31:26 +02:00
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
_Variadic_union<_Types...> _M_u;
|
2017-01-11 12:23:43 +01:00
|
|
|
using __index_type = __select_index<_Types...>;
|
|
|
|
__index_type _M_index;
|
2016-08-18 22:31:26 +02:00
|
|
|
};
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
template<typename... _Types>
|
|
|
|
struct _Variant_storage<true, _Types...>
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr _Variant_storage() : _M_index(variant_npos) { }
|
2016-08-18 22:31:26 +02:00
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
template<size_t _Np, typename... _Args>
|
|
|
|
constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
|
|
|
|
: _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
|
|
|
|
_M_index(_Np)
|
|
|
|
{ }
|
|
|
|
|
2016-12-06 12:26:48 +01:00
|
|
|
void _M_reset()
|
|
|
|
{ _M_index = variant_npos; }
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
_Variadic_union<_Types...> _M_u;
|
2017-01-11 12:23:43 +01:00
|
|
|
using __index_type = __select_index<_Types...>;
|
|
|
|
__index_type _M_index;
|
2016-08-18 22:31:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Helps SFINAE on special member functions. Otherwise it can live in variant
|
|
|
|
// class.
|
|
|
|
template<typename... _Types>
|
|
|
|
struct _Variant_base :
|
2016-12-06 12:20:13 +01:00
|
|
|
_Variant_storage<(std::is_trivially_destructible_v<_Types> && ...),
|
|
|
|
_Types...>
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
2016-12-06 12:20:13 +01:00
|
|
|
using _Storage =
|
|
|
|
_Variant_storage<(std::is_trivially_destructible_v<_Types> && ...),
|
|
|
|
_Types...>;
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
constexpr
|
|
|
|
_Variant_base()
|
|
|
|
noexcept(is_nothrow_default_constructible_v<
|
|
|
|
variant_alternative_t<0, variant<_Types...>>>)
|
Implement P0504R0 (Revisiting in-place tag types for any/optional/variant).
Implement P0504R0 (Revisiting in-place tag types for
any/optional/variant).
* include/std/any (any(_ValueType&& __value)): Constrain
the __is_in_place_type with the decayed type.
(make_any): Adjust to use the new tag type.
* include/std/utility (in_place_tag): Remove.
(in_place_t): Turn into a non-reference tag type.
(__in_place, __in_place_type, __in_place_index): Remove.
(in_place): Turn into an inline variable of non-reference
tag type.
(in_place<_Tp>): Remove.
(in_place_index<_Idx>): Remove.
(in_place_type_t): New.
(in_place_type): Turn into a variable template of non-reference
type.
(in_place_index_t): New.
(in_place_index): Turn into a variable template of non-reference
type.
* include/std/variant
(_Variant_storage(in_place_index_t<_Np>, _Args&&...)): Adjust to
use the new tag type.
(_Union(in_place_index_t<0>, _Args&&...)): Likewise.
(_Union(in_place_index_t<_Np>, _Args&&...)): Likewise.
(_Variant_base()): Likewise.
(variant(_Tp&&)): Likewise.
(variant(in_place_type_t<_Tp>, _Args&&...)): Likewise.
(variant(in_place_type_t<_Tp>, initializer_list<_Up>,
_Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, _Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, initializer_list<_Up>,
_Args&&...)): Likewise
(variant(allocator_arg_t, const _Alloc&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, _Tp&&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
initializer_list<_Up>, _Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
initializer_list<_Up>, _Args&&...)): Likewise.
(emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/cons/explicit.cc: Likewise.
* testsuite/20_util/any/cons/in_place.cc: Likewise.
* testsuite/20_util/any/requirements.cc: Add tests to
check that any is not constructible from the new in_place_type_t
of any value category.
* testsuite/20_util/in_place/requirements.cc: Adjust to
use the new tag type.
* testsuite/20_util/variant/compile.cc: Likewise.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r242401
2016-11-14 21:47:44 +01:00
|
|
|
: _Variant_base(in_place_index<0>) { }
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
_Variant_base(const _Variant_base& __rhs)
|
|
|
|
{
|
|
|
|
if (__rhs._M_valid())
|
|
|
|
{
|
|
|
|
static constexpr void (*_S_vtable[])(void*, void*) =
|
2016-12-06 12:28:09 +01:00
|
|
|
{ &__erased_ctor<_Types&, const _Types&>... };
|
2016-08-18 22:31:26 +02:00
|
|
|
_S_vtable[__rhs._M_index](_M_storage(), __rhs._M_storage());
|
2016-12-06 12:20:13 +01:00
|
|
|
this->_M_index = __rhs._M_index;
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_Variant_base(_Variant_base&& __rhs)
|
2017-01-06 16:27:01 +01:00
|
|
|
noexcept((is_nothrow_move_constructible_v<_Types> && ...))
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
if (__rhs._M_valid())
|
|
|
|
{
|
|
|
|
static constexpr void (*_S_vtable[])(void*, void*) =
|
2016-12-06 12:28:09 +01:00
|
|
|
{ &__erased_ctor<_Types&, _Types&&>... };
|
2016-08-18 22:31:26 +02:00
|
|
|
_S_vtable[__rhs._M_index](_M_storage(), __rhs._M_storage());
|
2016-12-06 12:20:13 +01:00
|
|
|
this->_M_index = __rhs._M_index;
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Args>
|
|
|
|
constexpr explicit
|
|
|
|
_Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
|
2016-12-06 12:20:13 +01:00
|
|
|
: _Storage(__i, std::forward<_Args>(__args)...)
|
2016-08-18 22:31:26 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
_Variant_base&
|
|
|
|
operator=(const _Variant_base& __rhs)
|
|
|
|
{
|
2016-12-06 12:20:13 +01:00
|
|
|
if (this->_M_index == __rhs._M_index)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
if (__rhs._M_valid())
|
|
|
|
{
|
|
|
|
static constexpr void (*_S_vtable[])(void*, void*) =
|
2016-12-06 12:28:09 +01:00
|
|
|
{ &__erased_assign<_Types&, const _Types&>... };
|
2016-08-18 22:31:26 +02:00
|
|
|
_S_vtable[__rhs._M_index](_M_storage(), __rhs._M_storage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_Variant_base __tmp(__rhs);
|
|
|
|
this->~_Variant_base();
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
::new (this) _Variant_base(std::move(__tmp));
|
|
|
|
}
|
|
|
|
__catch (...)
|
|
|
|
{
|
2016-12-06 12:20:13 +01:00
|
|
|
this->_M_index = variant_npos;
|
2016-08-18 22:31:26 +02:00
|
|
|
__throw_exception_again;
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 12:20:13 +01:00
|
|
|
__glibcxx_assert(this->_M_index == __rhs._M_index);
|
2016-08-18 22:31:26 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-12-06 12:26:48 +01:00
|
|
|
void _M_destructive_move(_Variant_base&& __rhs)
|
|
|
|
{
|
|
|
|
this->~_Variant_base();
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
::new (this) _Variant_base(std::move(__rhs));
|
|
|
|
}
|
|
|
|
__catch (...)
|
|
|
|
{
|
|
|
|
this->_M_index = variant_npos;
|
|
|
|
__throw_exception_again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
_Variant_base&
|
|
|
|
operator=(_Variant_base&& __rhs)
|
2017-01-06 16:27:01 +01:00
|
|
|
noexcept((is_nothrow_move_constructible_v<_Types> && ...)
|
|
|
|
&& (is_nothrow_move_assignable_v<_Types> && ...))
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
2016-12-06 12:20:13 +01:00
|
|
|
if (this->_M_index == __rhs._M_index)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
if (__rhs._M_valid())
|
|
|
|
{
|
|
|
|
static constexpr void (*_S_vtable[])(void*, void*) =
|
2016-12-06 12:28:09 +01:00
|
|
|
{ &__erased_assign<_Types&, _Types&&>... };
|
2016-08-18 22:31:26 +02:00
|
|
|
_S_vtable[__rhs._M_index](_M_storage(), __rhs._M_storage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-06 12:26:48 +01:00
|
|
|
_M_destructive_move(std::move(__rhs));
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
void*
|
|
|
|
_M_storage() const
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
2016-12-06 12:20:13 +01:00
|
|
|
return const_cast<void*>(static_cast<const void*>(
|
|
|
|
std::addressof(_Storage::_M_u)));
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool
|
|
|
|
_M_valid() const noexcept
|
2017-01-11 12:23:43 +01:00
|
|
|
{
|
|
|
|
return this->_M_index !=
|
|
|
|
typename _Storage::__index_type(variant_npos);
|
|
|
|
}
|
2016-08-18 22:31:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// For how many times does _Tp appear in _Tuple?
|
|
|
|
template<typename _Tp, typename _Tuple>
|
|
|
|
struct __tuple_count;
|
|
|
|
|
|
|
|
template<typename _Tp, typename _Tuple>
|
Implement P0607R0 "Inline Variables for Standard Library" for C++17
2017-03-23 Daniel Kruegler <daniel.kruegler@gmail.com>
* include/bits/c++config (_GLIBCXX17_INLINE): Define.
* include/bits/regex_constants.h (All std::regex_constants constants):
Add _GLIBCXX17_INLINE as per P0607R0.
* include/bits/std_mutex.h (defer_lock, try_to_lock, adopt_lock):
Likewise.
* include/bits/stl_pair.h (piecewise_construct): Likewise.
* include/bits/uses_allocator.h (allocator_arg, uses_allocator_v)
(__is_uses_allocator_constructible_v)
(__is_nothrow_uses_allocator_constructible_v): Likewise.
* include/std/chrono (treat_as_floating_point_v): Likewise.
* include/std/functional (is_bind_expression_v, is_placeholder_v):
Likewise.
* include/std/optional (nullopt): Likewise.
* include/std/ratio (ratio_equal_v, ratio_not_equal_v, ratio_less_v)
ratio_less_equal_v, ratio_greater_v, ratio_greater_equal_v): Likewise.
* include/std/system_error (is_error_code_enum_v)
(is_error_condition_enum_v): Likewise.
* include/std/tuple (tuple_size_v, ignore): Likewise.
(ignore): Declare ignore constexpr as per LWG 2773, declare assignment
constexpr as per LWG 2933.
* include/std/type_traits (All variable templates): Add
_GLIBCXX17_INLINE as per P0607R0.
* include/std/variant (variant_size_v, variant_npos, __index_of_v)
(__tuple_count_v, __exactly_once): Likewise.
* testsuite/18_support/headers/new/synopsis.cc
(hardware_destructive_interference_size)
(hardware_constructive_interference_size): Likewise for commented-out
variables.
* testsuite/20_util/tuple/creation_functions/constexpr.cc: Add new
test function for constexpr std::ignore (LWG 2773).
* testsuite/20_util/tuple/creation_functions/constexpr_cpp14.cc: New
test for LWG 2933.
From-SVN: r246423
2017-03-23 20:40:07 +01:00
|
|
|
inline constexpr size_t __tuple_count_v =
|
|
|
|
__tuple_count<_Tp, _Tuple>::value;
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<typename _Tp, typename... _Types>
|
|
|
|
struct __tuple_count<_Tp, tuple<_Types...>>
|
|
|
|
: integral_constant<size_t, 0> { };
|
|
|
|
|
|
|
|
template<typename _Tp, typename _First, typename... _Rest>
|
|
|
|
struct __tuple_count<_Tp, tuple<_First, _Rest...>>
|
|
|
|
: integral_constant<
|
|
|
|
size_t,
|
|
|
|
__tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
|
|
|
|
|
|
|
|
// TODO: Reuse this in <tuple> ?
|
|
|
|
template<typename _Tp, typename... _Types>
|
Implement P0607R0 "Inline Variables for Standard Library" for C++17
2017-03-23 Daniel Kruegler <daniel.kruegler@gmail.com>
* include/bits/c++config (_GLIBCXX17_INLINE): Define.
* include/bits/regex_constants.h (All std::regex_constants constants):
Add _GLIBCXX17_INLINE as per P0607R0.
* include/bits/std_mutex.h (defer_lock, try_to_lock, adopt_lock):
Likewise.
* include/bits/stl_pair.h (piecewise_construct): Likewise.
* include/bits/uses_allocator.h (allocator_arg, uses_allocator_v)
(__is_uses_allocator_constructible_v)
(__is_nothrow_uses_allocator_constructible_v): Likewise.
* include/std/chrono (treat_as_floating_point_v): Likewise.
* include/std/functional (is_bind_expression_v, is_placeholder_v):
Likewise.
* include/std/optional (nullopt): Likewise.
* include/std/ratio (ratio_equal_v, ratio_not_equal_v, ratio_less_v)
ratio_less_equal_v, ratio_greater_v, ratio_greater_equal_v): Likewise.
* include/std/system_error (is_error_code_enum_v)
(is_error_condition_enum_v): Likewise.
* include/std/tuple (tuple_size_v, ignore): Likewise.
(ignore): Declare ignore constexpr as per LWG 2773, declare assignment
constexpr as per LWG 2933.
* include/std/type_traits (All variable templates): Add
_GLIBCXX17_INLINE as per P0607R0.
* include/std/variant (variant_size_v, variant_npos, __index_of_v)
(__tuple_count_v, __exactly_once): Likewise.
* testsuite/18_support/headers/new/synopsis.cc
(hardware_destructive_interference_size)
(hardware_constructive_interference_size): Likewise for commented-out
variables.
* testsuite/20_util/tuple/creation_functions/constexpr.cc: Add new
test function for constexpr std::ignore (LWG 2773).
* testsuite/20_util/tuple/creation_functions/constexpr_cpp14.cc: New
test for LWG 2933.
From-SVN: r246423
2017-03-23 20:40:07 +01:00
|
|
|
inline constexpr bool __exactly_once =
|
|
|
|
__tuple_count_v<_Tp, tuple<_Types...>> == 1;
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
// Takes _Types and create an overloaded _S_fun for each type.
|
|
|
|
// If a type appears more than once in _Types, create only one overload.
|
|
|
|
template<typename... _Types>
|
|
|
|
struct __overload_set
|
|
|
|
{ static void _S_fun(); };
|
|
|
|
|
|
|
|
template<typename _First, typename... _Rest>
|
|
|
|
struct __overload_set<_First, _Rest...> : __overload_set<_Rest...>
|
|
|
|
{
|
|
|
|
using __overload_set<_Rest...>::_S_fun;
|
|
|
|
static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... _Rest>
|
|
|
|
struct __overload_set<void, _Rest...> : __overload_set<_Rest...>
|
|
|
|
{
|
|
|
|
using __overload_set<_Rest...>::_S_fun;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper for variant(_Tp&&) and variant::operator=(_Tp&&).
|
|
|
|
// __accepted_index maps the arbitrary _Tp to an alternative type in _Variant.
|
|
|
|
template<typename _Tp, typename _Variant, typename = void>
|
|
|
|
struct __accepted_index
|
|
|
|
{ static constexpr size_t value = variant_npos; };
|
|
|
|
|
|
|
|
template<typename _Tp, typename... _Types>
|
|
|
|
struct __accepted_index<
|
|
|
|
_Tp, variant<_Types...>,
|
|
|
|
decltype(__overload_set<_Types...>::_S_fun(std::declval<_Tp>()),
|
|
|
|
std::declval<void>())>
|
|
|
|
{
|
|
|
|
static constexpr size_t value = sizeof...(_Types) - 1
|
|
|
|
- decltype(__overload_set<_Types...>::
|
|
|
|
_S_fun(std::declval<_Tp>()))::value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns the raw storage for __v.
|
|
|
|
template<typename _Variant>
|
|
|
|
void* __get_storage(_Variant&& __v)
|
|
|
|
{ return __v._M_storage(); }
|
|
|
|
|
|
|
|
// Used for storing multi-dimensional vtable.
|
|
|
|
template<typename _Tp, size_t... _Dimensions>
|
|
|
|
struct _Multi_array
|
|
|
|
{
|
|
|
|
constexpr const _Tp&
|
|
|
|
_M_access() const
|
|
|
|
{ return _M_data; }
|
|
|
|
|
|
|
|
_Tp _M_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename _Tp, size_t __first, size_t... __rest>
|
|
|
|
struct _Multi_array<_Tp, __first, __rest...>
|
|
|
|
{
|
|
|
|
template<typename... _Args>
|
|
|
|
constexpr const _Tp&
|
|
|
|
_M_access(size_t __first_index, _Args... __rest_indices) const
|
|
|
|
{ return _M_arr[__first_index]._M_access(__rest_indices...); }
|
|
|
|
|
|
|
|
_Multi_array<_Tp, __rest...> _M_arr[__first];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Creates a multi-dimensional vtable recursively.
|
|
|
|
//
|
|
|
|
// For example,
|
|
|
|
// visit([](auto, auto){},
|
2016-12-06 12:28:09 +01:00
|
|
|
// variant<int, char>(), // typedef'ed as V1
|
|
|
|
// variant<float, double, long double>()) // typedef'ed as V2
|
2016-08-18 22:31:26 +02:00
|
|
|
// will trigger instantiations of:
|
2016-12-06 12:28:09 +01:00
|
|
|
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
|
|
|
|
// tuple<V1&&, V2&&>, std::index_sequence<>>
|
|
|
|
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
|
|
|
|
// tuple<V1&&, V2&&>, std::index_sequence<0>>
|
|
|
|
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
|
|
|
|
// tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
|
|
|
|
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
|
|
|
|
// tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
|
|
|
|
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
|
|
|
|
// tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
|
|
|
|
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
|
|
|
|
// tuple<V1&&, V2&&>, std::index_sequence<1>>
|
|
|
|
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
|
|
|
|
// tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
|
|
|
|
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
|
|
|
|
// tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
|
|
|
|
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
|
|
|
|
// tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
|
2016-08-18 22:31:26 +02:00
|
|
|
// The returned multi-dimensional vtable can be fast accessed by the visitor
|
|
|
|
// using index calculation.
|
2016-12-06 12:28:09 +01:00
|
|
|
template<typename _Array_type, typename _Variant_tuple, typename _Index_seq>
|
2016-08-18 22:31:26 +02:00
|
|
|
struct __gen_vtable_impl;
|
|
|
|
|
2017-01-05 04:18:17 +01:00
|
|
|
template<typename _Result_type, typename _Visitor, size_t... __dimensions,
|
2016-12-06 12:28:09 +01:00
|
|
|
typename... _Variants, size_t... __indices>
|
|
|
|
struct __gen_vtable_impl<
|
2017-01-05 04:18:17 +01:00
|
|
|
_Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
|
2016-12-06 12:28:09 +01:00
|
|
|
tuple<_Variants...>, std::index_sequence<__indices...>>
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
2016-12-06 12:28:09 +01:00
|
|
|
using _Next =
|
|
|
|
remove_reference_t<typename _Nth_type<sizeof...(__indices),
|
|
|
|
_Variants...>::type>;
|
|
|
|
using _Array_type =
|
2017-01-05 04:18:17 +01:00
|
|
|
_Multi_array<_Result_type (*)(_Visitor, _Variants...),
|
|
|
|
__dimensions...>;
|
2016-12-06 12:28:09 +01:00
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
static constexpr _Array_type
|
|
|
|
_S_apply()
|
|
|
|
{
|
|
|
|
_Array_type __vtable{};
|
|
|
|
_S_apply_all_alts(
|
2016-12-06 12:28:09 +01:00
|
|
|
__vtable, make_index_sequence<variant_size_v<_Next>>());
|
2016-08-18 22:31:26 +02:00
|
|
|
return __vtable;
|
|
|
|
}
|
|
|
|
|
2016-12-06 12:28:09 +01:00
|
|
|
template<size_t... __var_indices>
|
2016-08-18 22:31:26 +02:00
|
|
|
static constexpr void
|
2016-12-06 12:28:09 +01:00
|
|
|
_S_apply_all_alts(_Array_type& __vtable,
|
|
|
|
std::index_sequence<__var_indices...>)
|
|
|
|
{
|
|
|
|
(_S_apply_single_alt<__var_indices>(
|
|
|
|
__vtable._M_arr[__var_indices]), ...);
|
|
|
|
}
|
2016-08-18 22:31:26 +02:00
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
template<size_t __index, typename _Tp>
|
2016-08-18 22:31:26 +02:00
|
|
|
static constexpr void
|
2016-12-06 12:20:13 +01:00
|
|
|
_S_apply_single_alt(_Tp& __element)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
2016-12-06 12:28:09 +01:00
|
|
|
using _Alternative = variant_alternative_t<__index, _Next>;
|
2016-08-18 22:31:26 +02:00
|
|
|
__element = __gen_vtable_impl<
|
2016-12-06 12:28:09 +01:00
|
|
|
remove_reference_t<
|
|
|
|
decltype(__element)>, tuple<_Variants...>,
|
|
|
|
std::index_sequence<__indices..., __index>>::_S_apply();
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-06 12:28:09 +01:00
|
|
|
template<typename _Result_type, typename _Visitor, typename... _Variants,
|
|
|
|
size_t... __indices>
|
2016-08-18 22:31:26 +02:00
|
|
|
struct __gen_vtable_impl<
|
2016-12-06 12:28:09 +01:00
|
|
|
_Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
|
|
|
|
tuple<_Variants...>, std::index_sequence<__indices...>>
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
using _Array_type =
|
2016-12-06 12:28:09 +01:00
|
|
|
_Multi_array<_Result_type (*)(_Visitor&&, _Variants...)>;
|
|
|
|
|
|
|
|
decltype(auto)
|
|
|
|
static constexpr __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
|
|
|
|
{
|
|
|
|
return __invoke(std::forward<_Visitor>(__visitor),
|
|
|
|
std::get<__indices>(
|
|
|
|
std::forward<_Variants>(__vars))...);
|
|
|
|
}
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
static constexpr auto
|
|
|
|
_S_apply()
|
2016-12-06 12:28:09 +01:00
|
|
|
{ return _Array_type{&__visit_invoke}; }
|
2016-08-18 22:31:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename _Result_type, typename _Visitor, typename... _Variants>
|
|
|
|
struct __gen_vtable
|
|
|
|
{
|
2016-12-06 12:28:09 +01:00
|
|
|
using _Func_ptr = _Result_type (*)(_Visitor&&, _Variants...);
|
2016-08-18 22:31:26 +02:00
|
|
|
using _Array_type =
|
2016-12-06 12:28:09 +01:00
|
|
|
_Multi_array<_Func_ptr,
|
|
|
|
variant_size_v<remove_reference_t<_Variants>>...>;
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
static constexpr _Array_type
|
|
|
|
_S_apply()
|
|
|
|
{
|
2016-12-06 12:28:09 +01:00
|
|
|
return __gen_vtable_impl<_Array_type, tuple<_Variants...>,
|
|
|
|
std::index_sequence<>>::_S_apply();
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
2016-12-06 12:28:09 +01:00
|
|
|
|
|
|
|
static constexpr auto _S_vtable = _S_apply();
|
2016-08-18 22:31:26 +02:00
|
|
|
};
|
|
|
|
|
2016-12-06 12:26:48 +01:00
|
|
|
template<size_t _Np, typename _Tp>
|
|
|
|
struct _Base_dedup : public _Tp { };
|
|
|
|
|
|
|
|
template<typename _Variant, typename __indices>
|
|
|
|
struct _Variant_hash_base;
|
|
|
|
|
|
|
|
template<typename... _Types, size_t... __indices>
|
|
|
|
struct _Variant_hash_base<variant<_Types...>,
|
|
|
|
std::index_sequence<__indices...>>
|
|
|
|
: _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
|
|
|
|
|
2016-10-03 16:35:28 +02:00
|
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
2016-08-18 22:31:26 +02:00
|
|
|
} // namespace __variant
|
|
|
|
} // namespace __detail
|
|
|
|
|
2016-10-03 16:35:28 +02:00
|
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
template<typename _Tp, typename... _Types>
|
|
|
|
inline constexpr bool holds_alternative(const variant<_Types...>& __v)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
|
|
|
|
"T should occur for exactly once in alternatives");
|
|
|
|
return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _Tp, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr inline _Tp& get(variant<_Types...>& __v)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
|
|
|
|
"T should occur for exactly once in alternatives");
|
|
|
|
static_assert(!is_void_v<_Tp>, "_Tp should not be void");
|
|
|
|
return get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _Tp, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr inline _Tp&& get(variant<_Types...>&& __v)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
|
|
|
|
"T should occur for exactly once in alternatives");
|
|
|
|
static_assert(!is_void_v<_Tp>, "_Tp should not be void");
|
|
|
|
return get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
|
|
|
|
std::move(__v));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _Tp, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr inline const _Tp& get(const variant<_Types...>& __v)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
|
|
|
|
"T should occur for exactly once in alternatives");
|
|
|
|
static_assert(!is_void_v<_Tp>, "_Tp should not be void");
|
|
|
|
return get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _Tp, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr inline const _Tp&& get(const variant<_Types...>&& __v)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
|
|
|
|
"T should occur for exactly once in alternatives");
|
|
|
|
static_assert(!is_void_v<_Tp>, "_Tp should not be void");
|
|
|
|
return get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
|
|
|
|
std::move(__v));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr inline
|
|
|
|
add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
|
2016-08-18 22:31:26 +02:00
|
|
|
get_if(variant<_Types...>* __ptr) noexcept
|
|
|
|
{
|
|
|
|
using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
|
|
|
|
static_assert(_Np < sizeof...(_Types),
|
|
|
|
"The index should be in [0, number of alternatives)");
|
|
|
|
static_assert(!is_void_v<_Alternative_type>, "_Tp should not be void");
|
|
|
|
if (__ptr && __ptr->index() == _Np)
|
2016-12-06 12:20:13 +01:00
|
|
|
return &__detail::__variant::__get<_Np>(*__ptr);
|
2016-08-18 22:31:26 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr inline
|
|
|
|
add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
|
2016-08-18 22:31:26 +02:00
|
|
|
get_if(const variant<_Types...>* __ptr) noexcept
|
|
|
|
{
|
|
|
|
using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
|
|
|
|
static_assert(_Np < sizeof...(_Types),
|
|
|
|
"The index should be in [0, number of alternatives)");
|
|
|
|
static_assert(!is_void_v<_Alternative_type>, "_Tp should not be void");
|
|
|
|
if (__ptr && __ptr->index() == _Np)
|
2016-12-06 12:20:13 +01:00
|
|
|
return &__detail::__variant::__get<_Np>(*__ptr);
|
2016-08-18 22:31:26 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _Tp, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr inline add_pointer_t<_Tp>
|
|
|
|
get_if(variant<_Types...>* __ptr) noexcept
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
|
|
|
|
"T should occur for exactly once in alternatives");
|
|
|
|
static_assert(!is_void_v<_Tp>, "_Tp should not be void");
|
|
|
|
return get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(__ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _Tp, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr inline add_pointer_t<const _Tp>
|
|
|
|
get_if(const variant<_Types...>* __ptr)
|
2016-08-18 22:31:26 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
|
|
|
|
"T should occur for exactly once in alternatives");
|
|
|
|
static_assert(!is_void_v<_Tp>, "_Tp should not be void");
|
|
|
|
return get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(__ptr);
|
|
|
|
}
|
|
|
|
|
2017-02-15 10:01:06 +01:00
|
|
|
struct monostate { };
|
2016-08-18 22:31:26 +02:00
|
|
|
|
2017-02-15 10:01:06 +01:00
|
|
|
#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
|
|
|
|
template<typename... _Types> \
|
|
|
|
constexpr bool operator __OP(const variant<_Types...>& __lhs, \
|
|
|
|
const variant<_Types...>& __rhs) \
|
|
|
|
{ \
|
|
|
|
return __lhs._M_##__NAME(__rhs, std::index_sequence_for<_Types...>{}); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
constexpr bool operator __OP(monostate, monostate) noexcept \
|
|
|
|
{ return 0 __OP 0; }
|
|
|
|
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
|
|
|
|
|
|
|
|
#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<typename _Visitor, typename... _Variants>
|
2016-12-06 12:28:09 +01:00
|
|
|
constexpr decltype(auto) visit(_Visitor&&, _Variants&&...);
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<typename... _Types>
|
2016-12-06 12:26:48 +01:00
|
|
|
inline enable_if_t<(is_move_constructible_v<_Types> && ...)
|
|
|
|
&& (is_swappable_v<_Types> && ...)>
|
Implement LWG 2766,
Swapping non-swappable types and LWG 2749,
swappable traits for variants.
* include/bits/move.h (swap(_Tp&, _Tp&)): Constrain
with __is_tuple_like.
* include/bits/stl_pair.h (swap(pair<_T1, _T2>&, pair<_T1, _T2>&)):
Add a deleted overload.
* include/bits/unique_ptr.h
(swap(unique_ptr<_Tp, _Dp>&, unique_ptr<_Tp, _Dp>&)): Likewise.
* include/std/array
(swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&)): Likewise.
* include/std/optional
(swap(optional<_Tp>&, optional<_Tp>&)): Likewise.
* include/std/tuple (__is_tuple_like_impl, __is_tuple_like):
Move to type_traits.
(swap(tuple<_Elements...>&, tuple<_Elements...>&)): Add a deleted
overload.
* include/std/type_traits (__is_tuple_like_impl, __is_tuple_like):
New.
(swap(_Tp&, _Tp&)): Constrain with __is_tuple_like.
* include/std/utility (__is_tuple_like_impl): Move to type_traits.
* include/std/variant
(swap(variant<_Types...>&, variant<_Types...>&)):
Add a deleted overload.
* testsuite/20_util/optional/swap/2.cc: Add tests for disabled
swaps.
* testsuite/20_util/pair/swap_cxx17.cc: New.
* testsuite/20_util/tuple/swap_cxx17.cc: Likewise.
* testsuite/20_util/unique_ptr/specialized_algorithms/swap_cxx17.cc:
Likewise.
* testsuite/20_util/variant/compile.cc: Add tests for disabled
swaps.
* testsuite/23_containers/array/specialized_algorithms/swap_cxx17.cc:
New.
* testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust.
* testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc:
Likewise.
From-SVN: r243120
2016-12-01 17:23:21 +01:00
|
|
|
swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
|
|
|
|
noexcept(noexcept(__lhs.swap(__rhs)))
|
2016-08-18 22:31:26 +02:00
|
|
|
{ __lhs.swap(__rhs); }
|
|
|
|
|
Implement LWG 2766,
Swapping non-swappable types and LWG 2749,
swappable traits for variants.
* include/bits/move.h (swap(_Tp&, _Tp&)): Constrain
with __is_tuple_like.
* include/bits/stl_pair.h (swap(pair<_T1, _T2>&, pair<_T1, _T2>&)):
Add a deleted overload.
* include/bits/unique_ptr.h
(swap(unique_ptr<_Tp, _Dp>&, unique_ptr<_Tp, _Dp>&)): Likewise.
* include/std/array
(swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&)): Likewise.
* include/std/optional
(swap(optional<_Tp>&, optional<_Tp>&)): Likewise.
* include/std/tuple (__is_tuple_like_impl, __is_tuple_like):
Move to type_traits.
(swap(tuple<_Elements...>&, tuple<_Elements...>&)): Add a deleted
overload.
* include/std/type_traits (__is_tuple_like_impl, __is_tuple_like):
New.
(swap(_Tp&, _Tp&)): Constrain with __is_tuple_like.
* include/std/utility (__is_tuple_like_impl): Move to type_traits.
* include/std/variant
(swap(variant<_Types...>&, variant<_Types...>&)):
Add a deleted overload.
* testsuite/20_util/optional/swap/2.cc: Add tests for disabled
swaps.
* testsuite/20_util/pair/swap_cxx17.cc: New.
* testsuite/20_util/tuple/swap_cxx17.cc: Likewise.
* testsuite/20_util/unique_ptr/specialized_algorithms/swap_cxx17.cc:
Likewise.
* testsuite/20_util/variant/compile.cc: Add tests for disabled
swaps.
* testsuite/23_containers/array/specialized_algorithms/swap_cxx17.cc:
New.
* testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust.
* testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc:
Likewise.
From-SVN: r243120
2016-12-01 17:23:21 +01:00
|
|
|
template<typename... _Types>
|
2017-01-06 16:27:01 +01:00
|
|
|
enable_if_t<!((is_move_constructible_v<_Types> && ...)
|
|
|
|
&& (is_swappable_v<_Types> && ...))>
|
Implement LWG 2766,
Swapping non-swappable types and LWG 2749,
swappable traits for variants.
* include/bits/move.h (swap(_Tp&, _Tp&)): Constrain
with __is_tuple_like.
* include/bits/stl_pair.h (swap(pair<_T1, _T2>&, pair<_T1, _T2>&)):
Add a deleted overload.
* include/bits/unique_ptr.h
(swap(unique_ptr<_Tp, _Dp>&, unique_ptr<_Tp, _Dp>&)): Likewise.
* include/std/array
(swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&)): Likewise.
* include/std/optional
(swap(optional<_Tp>&, optional<_Tp>&)): Likewise.
* include/std/tuple (__is_tuple_like_impl, __is_tuple_like):
Move to type_traits.
(swap(tuple<_Elements...>&, tuple<_Elements...>&)): Add a deleted
overload.
* include/std/type_traits (__is_tuple_like_impl, __is_tuple_like):
New.
(swap(_Tp&, _Tp&)): Constrain with __is_tuple_like.
* include/std/utility (__is_tuple_like_impl): Move to type_traits.
* include/std/variant
(swap(variant<_Types...>&, variant<_Types...>&)):
Add a deleted overload.
* testsuite/20_util/optional/swap/2.cc: Add tests for disabled
swaps.
* testsuite/20_util/pair/swap_cxx17.cc: New.
* testsuite/20_util/tuple/swap_cxx17.cc: Likewise.
* testsuite/20_util/unique_ptr/specialized_algorithms/swap_cxx17.cc:
Likewise.
* testsuite/20_util/variant/compile.cc: Add tests for disabled
swaps.
* testsuite/23_containers/array/specialized_algorithms/swap_cxx17.cc:
New.
* testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust.
* testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc:
Likewise.
From-SVN: r243120
2016-12-01 17:23:21 +01:00
|
|
|
swap(variant<_Types...>&, variant<_Types...>&) = delete;
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
class bad_variant_access : public exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bad_variant_access() noexcept : _M_reason("Unknown reason") { }
|
|
|
|
const char* what() const noexcept override
|
|
|
|
{ return _M_reason; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bad_variant_access(const char* __reason) : _M_reason(__reason) { }
|
|
|
|
|
|
|
|
const char* _M_reason;
|
|
|
|
|
|
|
|
friend void __throw_bad_variant_access(const char* __what);
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void
|
|
|
|
__throw_bad_variant_access(const char* __what)
|
|
|
|
{ _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
|
|
|
|
|
|
|
|
template<typename... _Types>
|
|
|
|
class variant
|
|
|
|
: private __detail::__variant::_Variant_base<_Types...>,
|
|
|
|
private _Enable_default_constructor<
|
|
|
|
is_default_constructible_v<
|
|
|
|
variant_alternative_t<0, variant<_Types...>>>, variant<_Types...>>,
|
|
|
|
private _Enable_copy_move<
|
2017-01-06 16:27:01 +01:00
|
|
|
(is_copy_constructible_v<_Types> && ...),
|
|
|
|
(is_copy_constructible_v<_Types> && ...)
|
|
|
|
&& (is_move_constructible_v<_Types> && ...)
|
|
|
|
&& (is_copy_assignable_v<_Types> && ...),
|
|
|
|
(is_move_constructible_v<_Types> && ...),
|
|
|
|
(is_move_constructible_v<_Types> && ...)
|
|
|
|
&& (is_move_assignable_v<_Types> && ...),
|
2016-08-18 22:31:26 +02:00
|
|
|
variant<_Types...>>
|
|
|
|
{
|
|
|
|
private:
|
2016-11-15 18:26:59 +01:00
|
|
|
static_assert(sizeof...(_Types) > 0,
|
|
|
|
"variant must have at least one alternative");
|
|
|
|
static_assert(!(std::is_reference_v<_Types> || ...),
|
|
|
|
"variant must have no reference alternative");
|
|
|
|
static_assert(!(std::is_void_v<_Types> || ...),
|
|
|
|
"variant must have no void alternative");
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
using _Base = __detail::__variant::_Variant_base<_Types...>;
|
|
|
|
using _Default_ctor_enabler =
|
|
|
|
_Enable_default_constructor<
|
|
|
|
is_default_constructible_v<
|
|
|
|
variant_alternative_t<0, variant<_Types...>>>, variant<_Types...>>;
|
|
|
|
|
|
|
|
template<typename _Tp>
|
|
|
|
static constexpr bool
|
|
|
|
__exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
|
|
|
|
|
|
|
|
template<typename _Tp>
|
|
|
|
static constexpr size_t __accepted_index =
|
|
|
|
__detail::__variant::__accepted_index<_Tp&&, variant>::value;
|
|
|
|
|
|
|
|
template<size_t _Np, bool = _Np < sizeof...(_Types)>
|
|
|
|
struct __to_type_impl;
|
|
|
|
|
|
|
|
template<size_t _Np>
|
|
|
|
struct __to_type_impl<_Np, true>
|
|
|
|
{ using type = variant_alternative_t<_Np, variant>; };
|
|
|
|
|
|
|
|
template<size_t _Np>
|
|
|
|
using __to_type = typename __to_type_impl<_Np>::type;
|
|
|
|
|
|
|
|
template<typename _Tp>
|
|
|
|
using __accepted_type = __to_type<__accepted_index<_Tp>>;
|
|
|
|
|
|
|
|
template<typename _Tp>
|
|
|
|
static constexpr size_t __index_of =
|
|
|
|
__detail::__variant::__index_of_v<_Tp, _Types...>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr variant()
|
|
|
|
noexcept(is_nothrow_default_constructible_v<__to_type<0>>) = default;
|
|
|
|
variant(const variant&) = default;
|
|
|
|
variant(variant&&)
|
2017-01-06 16:27:01 +01:00
|
|
|
noexcept((is_nothrow_move_constructible_v<_Types> && ...)) = default;
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<typename _Tp,
|
2017-05-28 23:27:30 +02:00
|
|
|
typename = enable_if_t<!is_same_v<decay_t<_Tp>, variant>>,
|
2016-08-18 22:31:26 +02:00
|
|
|
typename = enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
|
2017-05-28 23:27:30 +02:00
|
|
|
&& is_constructible_v<__accepted_type<_Tp&&>, _Tp&&>>>
|
2016-08-18 22:31:26 +02:00
|
|
|
constexpr
|
|
|
|
variant(_Tp&& __t)
|
|
|
|
noexcept(is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp&&>)
|
Implement P0504R0 (Revisiting in-place tag types for any/optional/variant).
Implement P0504R0 (Revisiting in-place tag types for
any/optional/variant).
* include/std/any (any(_ValueType&& __value)): Constrain
the __is_in_place_type with the decayed type.
(make_any): Adjust to use the new tag type.
* include/std/utility (in_place_tag): Remove.
(in_place_t): Turn into a non-reference tag type.
(__in_place, __in_place_type, __in_place_index): Remove.
(in_place): Turn into an inline variable of non-reference
tag type.
(in_place<_Tp>): Remove.
(in_place_index<_Idx>): Remove.
(in_place_type_t): New.
(in_place_type): Turn into a variable template of non-reference
type.
(in_place_index_t): New.
(in_place_index): Turn into a variable template of non-reference
type.
* include/std/variant
(_Variant_storage(in_place_index_t<_Np>, _Args&&...)): Adjust to
use the new tag type.
(_Union(in_place_index_t<0>, _Args&&...)): Likewise.
(_Union(in_place_index_t<_Np>, _Args&&...)): Likewise.
(_Variant_base()): Likewise.
(variant(_Tp&&)): Likewise.
(variant(in_place_type_t<_Tp>, _Args&&...)): Likewise.
(variant(in_place_type_t<_Tp>, initializer_list<_Up>,
_Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, _Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, initializer_list<_Up>,
_Args&&...)): Likewise
(variant(allocator_arg_t, const _Alloc&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, _Tp&&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
initializer_list<_Up>, _Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
initializer_list<_Up>, _Args&&...)): Likewise.
(emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/cons/explicit.cc: Likewise.
* testsuite/20_util/any/cons/in_place.cc: Likewise.
* testsuite/20_util/any/requirements.cc: Add tests to
check that any is not constructible from the new in_place_type_t
of any value category.
* testsuite/20_util/in_place/requirements.cc: Adjust to
use the new tag type.
* testsuite/20_util/variant/compile.cc: Likewise.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r242401
2016-11-14 21:47:44 +01:00
|
|
|
: variant(in_place_index<__accepted_index<_Tp&&>>, std::forward<_Tp>(__t))
|
2016-08-18 22:31:26 +02:00
|
|
|
{ __glibcxx_assert(holds_alternative<__accepted_type<_Tp&&>>(*this)); }
|
|
|
|
|
|
|
|
template<typename _Tp, typename... _Args,
|
|
|
|
typename = enable_if_t<__exactly_once<_Tp>
|
|
|
|
&& is_constructible_v<_Tp, _Args&&...>>>
|
|
|
|
constexpr explicit
|
|
|
|
variant(in_place_type_t<_Tp>, _Args&&... __args)
|
Implement P0504R0 (Revisiting in-place tag types for any/optional/variant).
Implement P0504R0 (Revisiting in-place tag types for
any/optional/variant).
* include/std/any (any(_ValueType&& __value)): Constrain
the __is_in_place_type with the decayed type.
(make_any): Adjust to use the new tag type.
* include/std/utility (in_place_tag): Remove.
(in_place_t): Turn into a non-reference tag type.
(__in_place, __in_place_type, __in_place_index): Remove.
(in_place): Turn into an inline variable of non-reference
tag type.
(in_place<_Tp>): Remove.
(in_place_index<_Idx>): Remove.
(in_place_type_t): New.
(in_place_type): Turn into a variable template of non-reference
type.
(in_place_index_t): New.
(in_place_index): Turn into a variable template of non-reference
type.
* include/std/variant
(_Variant_storage(in_place_index_t<_Np>, _Args&&...)): Adjust to
use the new tag type.
(_Union(in_place_index_t<0>, _Args&&...)): Likewise.
(_Union(in_place_index_t<_Np>, _Args&&...)): Likewise.
(_Variant_base()): Likewise.
(variant(_Tp&&)): Likewise.
(variant(in_place_type_t<_Tp>, _Args&&...)): Likewise.
(variant(in_place_type_t<_Tp>, initializer_list<_Up>,
_Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, _Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, initializer_list<_Up>,
_Args&&...)): Likewise
(variant(allocator_arg_t, const _Alloc&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, _Tp&&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
initializer_list<_Up>, _Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
initializer_list<_Up>, _Args&&...)): Likewise.
(emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/cons/explicit.cc: Likewise.
* testsuite/20_util/any/cons/in_place.cc: Likewise.
* testsuite/20_util/any/requirements.cc: Add tests to
check that any is not constructible from the new in_place_type_t
of any value category.
* testsuite/20_util/in_place/requirements.cc: Adjust to
use the new tag type.
* testsuite/20_util/variant/compile.cc: Likewise.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r242401
2016-11-14 21:47:44 +01:00
|
|
|
: variant(in_place_index<__index_of<_Tp>>, std::forward<_Args>(__args)...)
|
2016-08-18 22:31:26 +02:00
|
|
|
{ __glibcxx_assert(holds_alternative<_Tp>(*this)); }
|
|
|
|
|
|
|
|
template<typename _Tp, typename _Up, typename... _Args,
|
|
|
|
typename = enable_if_t<__exactly_once<_Tp>
|
|
|
|
&& is_constructible_v<
|
|
|
|
_Tp, initializer_list<_Up>&, _Args&&...>>>
|
|
|
|
constexpr explicit
|
|
|
|
variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
|
|
|
|
_Args&&... __args)
|
Implement P0504R0 (Revisiting in-place tag types for any/optional/variant).
Implement P0504R0 (Revisiting in-place tag types for
any/optional/variant).
* include/std/any (any(_ValueType&& __value)): Constrain
the __is_in_place_type with the decayed type.
(make_any): Adjust to use the new tag type.
* include/std/utility (in_place_tag): Remove.
(in_place_t): Turn into a non-reference tag type.
(__in_place, __in_place_type, __in_place_index): Remove.
(in_place): Turn into an inline variable of non-reference
tag type.
(in_place<_Tp>): Remove.
(in_place_index<_Idx>): Remove.
(in_place_type_t): New.
(in_place_type): Turn into a variable template of non-reference
type.
(in_place_index_t): New.
(in_place_index): Turn into a variable template of non-reference
type.
* include/std/variant
(_Variant_storage(in_place_index_t<_Np>, _Args&&...)): Adjust to
use the new tag type.
(_Union(in_place_index_t<0>, _Args&&...)): Likewise.
(_Union(in_place_index_t<_Np>, _Args&&...)): Likewise.
(_Variant_base()): Likewise.
(variant(_Tp&&)): Likewise.
(variant(in_place_type_t<_Tp>, _Args&&...)): Likewise.
(variant(in_place_type_t<_Tp>, initializer_list<_Up>,
_Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, _Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, initializer_list<_Up>,
_Args&&...)): Likewise
(variant(allocator_arg_t, const _Alloc&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, _Tp&&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
initializer_list<_Up>, _Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
initializer_list<_Up>, _Args&&...)): Likewise.
(emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/cons/explicit.cc: Likewise.
* testsuite/20_util/any/cons/in_place.cc: Likewise.
* testsuite/20_util/any/requirements.cc: Add tests to
check that any is not constructible from the new in_place_type_t
of any value category.
* testsuite/20_util/in_place/requirements.cc: Adjust to
use the new tag type.
* testsuite/20_util/variant/compile.cc: Likewise.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r242401
2016-11-14 21:47:44 +01:00
|
|
|
: variant(in_place_index<__index_of<_Tp>>, __il,
|
2016-09-22 11:56:54 +02:00
|
|
|
std::forward<_Args>(__args)...)
|
2016-08-18 22:31:26 +02:00
|
|
|
{ __glibcxx_assert(holds_alternative<_Tp>(*this)); }
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Args,
|
|
|
|
typename = enable_if_t<
|
|
|
|
is_constructible_v<__to_type<_Np>, _Args&&...>>>
|
|
|
|
constexpr explicit
|
|
|
|
variant(in_place_index_t<_Np>, _Args&&... __args)
|
Implement P0504R0 (Revisiting in-place tag types for any/optional/variant).
Implement P0504R0 (Revisiting in-place tag types for
any/optional/variant).
* include/std/any (any(_ValueType&& __value)): Constrain
the __is_in_place_type with the decayed type.
(make_any): Adjust to use the new tag type.
* include/std/utility (in_place_tag): Remove.
(in_place_t): Turn into a non-reference tag type.
(__in_place, __in_place_type, __in_place_index): Remove.
(in_place): Turn into an inline variable of non-reference
tag type.
(in_place<_Tp>): Remove.
(in_place_index<_Idx>): Remove.
(in_place_type_t): New.
(in_place_type): Turn into a variable template of non-reference
type.
(in_place_index_t): New.
(in_place_index): Turn into a variable template of non-reference
type.
* include/std/variant
(_Variant_storage(in_place_index_t<_Np>, _Args&&...)): Adjust to
use the new tag type.
(_Union(in_place_index_t<0>, _Args&&...)): Likewise.
(_Union(in_place_index_t<_Np>, _Args&&...)): Likewise.
(_Variant_base()): Likewise.
(variant(_Tp&&)): Likewise.
(variant(in_place_type_t<_Tp>, _Args&&...)): Likewise.
(variant(in_place_type_t<_Tp>, initializer_list<_Up>,
_Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, _Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, initializer_list<_Up>,
_Args&&...)): Likewise
(variant(allocator_arg_t, const _Alloc&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, _Tp&&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
initializer_list<_Up>, _Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
initializer_list<_Up>, _Args&&...)): Likewise.
(emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/cons/explicit.cc: Likewise.
* testsuite/20_util/any/cons/in_place.cc: Likewise.
* testsuite/20_util/any/requirements.cc: Add tests to
check that any is not constructible from the new in_place_type_t
of any value category.
* testsuite/20_util/in_place/requirements.cc: Adjust to
use the new tag type.
* testsuite/20_util/variant/compile.cc: Likewise.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r242401
2016-11-14 21:47:44 +01:00
|
|
|
: _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
|
2016-08-18 22:31:26 +02:00
|
|
|
_Default_ctor_enabler(_Enable_default_constructor_tag{})
|
|
|
|
{ __glibcxx_assert(index() == _Np); }
|
|
|
|
|
|
|
|
template<size_t _Np, typename _Up, typename... _Args,
|
|
|
|
typename = enable_if_t<is_constructible_v<__to_type<_Np>,
|
|
|
|
initializer_list<_Up>&, _Args&&...>>>
|
|
|
|
constexpr explicit
|
|
|
|
variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
|
|
|
|
_Args&&... __args)
|
Implement P0504R0 (Revisiting in-place tag types for any/optional/variant).
Implement P0504R0 (Revisiting in-place tag types for
any/optional/variant).
* include/std/any (any(_ValueType&& __value)): Constrain
the __is_in_place_type with the decayed type.
(make_any): Adjust to use the new tag type.
* include/std/utility (in_place_tag): Remove.
(in_place_t): Turn into a non-reference tag type.
(__in_place, __in_place_type, __in_place_index): Remove.
(in_place): Turn into an inline variable of non-reference
tag type.
(in_place<_Tp>): Remove.
(in_place_index<_Idx>): Remove.
(in_place_type_t): New.
(in_place_type): Turn into a variable template of non-reference
type.
(in_place_index_t): New.
(in_place_index): Turn into a variable template of non-reference
type.
* include/std/variant
(_Variant_storage(in_place_index_t<_Np>, _Args&&...)): Adjust to
use the new tag type.
(_Union(in_place_index_t<0>, _Args&&...)): Likewise.
(_Union(in_place_index_t<_Np>, _Args&&...)): Likewise.
(_Variant_base()): Likewise.
(variant(_Tp&&)): Likewise.
(variant(in_place_type_t<_Tp>, _Args&&...)): Likewise.
(variant(in_place_type_t<_Tp>, initializer_list<_Up>,
_Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, _Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, initializer_list<_Up>,
_Args&&...)): Likewise
(variant(allocator_arg_t, const _Alloc&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, _Tp&&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
initializer_list<_Up>, _Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
initializer_list<_Up>, _Args&&...)): Likewise.
(emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/cons/explicit.cc: Likewise.
* testsuite/20_util/any/cons/in_place.cc: Likewise.
* testsuite/20_util/any/requirements.cc: Add tests to
check that any is not constructible from the new in_place_type_t
of any value category.
* testsuite/20_util/in_place/requirements.cc: Adjust to
use the new tag type.
* testsuite/20_util/variant/compile.cc: Likewise.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r242401
2016-11-14 21:47:44 +01:00
|
|
|
: _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
|
2016-08-18 22:31:26 +02:00
|
|
|
_Default_ctor_enabler(_Enable_default_constructor_tag{})
|
|
|
|
{ __glibcxx_assert(index() == _Np); }
|
|
|
|
|
|
|
|
~variant() = default;
|
|
|
|
|
|
|
|
variant& operator=(const variant&) = default;
|
|
|
|
variant& operator=(variant&&)
|
2017-01-06 16:27:01 +01:00
|
|
|
noexcept((is_nothrow_move_constructible_v<_Types> && ...)
|
|
|
|
&& (is_nothrow_move_assignable_v<_Types> && ...)) = default;
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
template<typename _Tp>
|
|
|
|
enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
|
|
|
|
&& is_constructible_v<__accepted_type<_Tp&&>, _Tp&&>
|
|
|
|
&& is_assignable_v<__accepted_type<_Tp&&>&, _Tp&&>
|
|
|
|
&& !is_same_v<decay_t<_Tp>, variant>, variant&>
|
|
|
|
operator=(_Tp&& __rhs)
|
|
|
|
noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp&&>
|
|
|
|
&& is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp&&>)
|
|
|
|
{
|
|
|
|
constexpr auto __index = __accepted_index<_Tp&&>;
|
|
|
|
if (index() == __index)
|
2016-09-22 10:45:55 +02:00
|
|
|
std::get<__index>(*this) = std::forward<_Tp>(__rhs);
|
2016-08-18 22:31:26 +02:00
|
|
|
else
|
2016-09-22 11:56:54 +02:00
|
|
|
this->emplace<__index>(std::forward<_Tp>(__rhs));
|
2016-08-18 22:31:26 +02:00
|
|
|
__glibcxx_assert(holds_alternative<__accepted_type<_Tp&&>>(*this));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _Tp, typename... _Args>
|
Implement LWG 2857, {variant,optional,any}::emplace should return the constructed value.
Implement LWG 2857, {variant,optional,any}::emplace should
return the constructed value.
* include/std/any (emplace(_Args&&...)): Change the return type and
return a reference to the constructed value.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/optional (emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
(emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
(emplace<_Np>(_Args&&...)): Likewise.
(emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/assign/emplace.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
* testsuite/20_util/optional/assignment/6.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r246182
2017-03-16 00:13:20 +01:00
|
|
|
enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
|
|
|
|
_Tp&>
|
2016-12-06 12:26:48 +01:00
|
|
|
emplace(_Args&&... __args)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
Implement LWG 2857, {variant,optional,any}::emplace should return the constructed value.
Implement LWG 2857, {variant,optional,any}::emplace should
return the constructed value.
* include/std/any (emplace(_Args&&...)): Change the return type and
return a reference to the constructed value.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/optional (emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
(emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
(emplace<_Np>(_Args&&...)): Likewise.
(emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/assign/emplace.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
* testsuite/20_util/optional/assignment/6.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r246182
2017-03-16 00:13:20 +01:00
|
|
|
auto& ret =
|
|
|
|
this->emplace<__index_of<_Tp>>(std::forward<_Args>(__args)...);
|
2016-08-18 22:31:26 +02:00
|
|
|
__glibcxx_assert(holds_alternative<_Tp>(*this));
|
Implement LWG 2857, {variant,optional,any}::emplace should return the constructed value.
Implement LWG 2857, {variant,optional,any}::emplace should
return the constructed value.
* include/std/any (emplace(_Args&&...)): Change the return type and
return a reference to the constructed value.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/optional (emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
(emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
(emplace<_Np>(_Args&&...)): Likewise.
(emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/assign/emplace.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
* testsuite/20_util/optional/assignment/6.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r246182
2017-03-16 00:13:20 +01:00
|
|
|
return ret;
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _Tp, typename _Up, typename... _Args>
|
2016-12-06 12:26:48 +01:00
|
|
|
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
|
Implement LWG 2857, {variant,optional,any}::emplace should return the constructed value.
Implement LWG 2857, {variant,optional,any}::emplace should
return the constructed value.
* include/std/any (emplace(_Args&&...)): Change the return type and
return a reference to the constructed value.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/optional (emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
(emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
(emplace<_Np>(_Args&&...)): Likewise.
(emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/assign/emplace.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
* testsuite/20_util/optional/assignment/6.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r246182
2017-03-16 00:13:20 +01:00
|
|
|
&& __exactly_once<_Tp>,
|
|
|
|
_Tp&>
|
2016-12-06 12:26:48 +01:00
|
|
|
emplace(initializer_list<_Up> __il, _Args&&... __args)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
Implement LWG 2857, {variant,optional,any}::emplace should return the constructed value.
Implement LWG 2857, {variant,optional,any}::emplace should
return the constructed value.
* include/std/any (emplace(_Args&&...)): Change the return type and
return a reference to the constructed value.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/optional (emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
(emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
(emplace<_Np>(_Args&&...)): Likewise.
(emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/assign/emplace.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
* testsuite/20_util/optional/assignment/6.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r246182
2017-03-16 00:13:20 +01:00
|
|
|
auto& ret =
|
|
|
|
this->emplace<__index_of<_Tp>>(__il,
|
|
|
|
std::forward<_Args>(__args)...);
|
2016-08-18 22:31:26 +02:00
|
|
|
__glibcxx_assert(holds_alternative<_Tp>(*this));
|
Implement LWG 2857, {variant,optional,any}::emplace should return the constructed value.
Implement LWG 2857, {variant,optional,any}::emplace should
return the constructed value.
* include/std/any (emplace(_Args&&...)): Change the return type and
return a reference to the constructed value.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/optional (emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
(emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
(emplace<_Np>(_Args&&...)): Likewise.
(emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/assign/emplace.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
* testsuite/20_util/optional/assignment/6.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r246182
2017-03-16 00:13:20 +01:00
|
|
|
return ret;
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Args>
|
2016-12-06 12:26:48 +01:00
|
|
|
enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
|
Implement LWG 2857, {variant,optional,any}::emplace should return the constructed value.
Implement LWG 2857, {variant,optional,any}::emplace should
return the constructed value.
* include/std/any (emplace(_Args&&...)): Change the return type and
return a reference to the constructed value.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/optional (emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
(emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
(emplace<_Np>(_Args&&...)): Likewise.
(emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/assign/emplace.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
* testsuite/20_util/optional/assignment/6.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r246182
2017-03-16 00:13:20 +01:00
|
|
|
_Args...>,
|
|
|
|
variant_alternative_t<_Np, variant>&>
|
2016-12-06 12:26:48 +01:00
|
|
|
emplace(_Args&&... __args)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
static_assert(_Np < sizeof...(_Types),
|
|
|
|
"The index should be in [0, number of alternatives)");
|
|
|
|
this->~variant();
|
|
|
|
__try
|
|
|
|
{
|
Implement P0504R0 (Revisiting in-place tag types for any/optional/variant).
Implement P0504R0 (Revisiting in-place tag types for
any/optional/variant).
* include/std/any (any(_ValueType&& __value)): Constrain
the __is_in_place_type with the decayed type.
(make_any): Adjust to use the new tag type.
* include/std/utility (in_place_tag): Remove.
(in_place_t): Turn into a non-reference tag type.
(__in_place, __in_place_type, __in_place_index): Remove.
(in_place): Turn into an inline variable of non-reference
tag type.
(in_place<_Tp>): Remove.
(in_place_index<_Idx>): Remove.
(in_place_type_t): New.
(in_place_type): Turn into a variable template of non-reference
type.
(in_place_index_t): New.
(in_place_index): Turn into a variable template of non-reference
type.
* include/std/variant
(_Variant_storage(in_place_index_t<_Np>, _Args&&...)): Adjust to
use the new tag type.
(_Union(in_place_index_t<0>, _Args&&...)): Likewise.
(_Union(in_place_index_t<_Np>, _Args&&...)): Likewise.
(_Variant_base()): Likewise.
(variant(_Tp&&)): Likewise.
(variant(in_place_type_t<_Tp>, _Args&&...)): Likewise.
(variant(in_place_type_t<_Tp>, initializer_list<_Up>,
_Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, _Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, initializer_list<_Up>,
_Args&&...)): Likewise
(variant(allocator_arg_t, const _Alloc&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, _Tp&&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
initializer_list<_Up>, _Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
initializer_list<_Up>, _Args&&...)): Likewise.
(emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/cons/explicit.cc: Likewise.
* testsuite/20_util/any/cons/in_place.cc: Likewise.
* testsuite/20_util/any/requirements.cc: Add tests to
check that any is not constructible from the new in_place_type_t
of any value category.
* testsuite/20_util/in_place/requirements.cc: Adjust to
use the new tag type.
* testsuite/20_util/variant/compile.cc: Likewise.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r242401
2016-11-14 21:47:44 +01:00
|
|
|
::new (this) variant(in_place_index<_Np>,
|
2016-09-22 11:56:54 +02:00
|
|
|
std::forward<_Args>(__args)...);
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
__catch (...)
|
|
|
|
{
|
|
|
|
this->_M_index = variant_npos;
|
|
|
|
__throw_exception_again;
|
|
|
|
}
|
|
|
|
__glibcxx_assert(index() == _Np);
|
Implement LWG 2857, {variant,optional,any}::emplace should return the constructed value.
Implement LWG 2857, {variant,optional,any}::emplace should
return the constructed value.
* include/std/any (emplace(_Args&&...)): Change the return type and
return a reference to the constructed value.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/optional (emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
(emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
(emplace<_Np>(_Args&&...)): Likewise.
(emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/assign/emplace.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
* testsuite/20_util/optional/assignment/6.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r246182
2017-03-16 00:13:20 +01:00
|
|
|
return std::get<_Np>(*this);
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t _Np, typename _Up, typename... _Args>
|
2016-12-06 12:26:48 +01:00
|
|
|
enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
|
Implement LWG 2857, {variant,optional,any}::emplace should return the constructed value.
Implement LWG 2857, {variant,optional,any}::emplace should
return the constructed value.
* include/std/any (emplace(_Args&&...)): Change the return type and
return a reference to the constructed value.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/optional (emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
(emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
(emplace<_Np>(_Args&&...)): Likewise.
(emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/assign/emplace.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
* testsuite/20_util/optional/assignment/6.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r246182
2017-03-16 00:13:20 +01:00
|
|
|
initializer_list<_Up>&, _Args...>,
|
|
|
|
variant_alternative_t<_Np, variant>&>
|
2016-12-06 12:26:48 +01:00
|
|
|
emplace(initializer_list<_Up> __il, _Args&&... __args)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
static_assert(_Np < sizeof...(_Types),
|
|
|
|
"The index should be in [0, number of alternatives)");
|
|
|
|
this->~variant();
|
|
|
|
__try
|
|
|
|
{
|
Implement P0504R0 (Revisiting in-place tag types for any/optional/variant).
Implement P0504R0 (Revisiting in-place tag types for
any/optional/variant).
* include/std/any (any(_ValueType&& __value)): Constrain
the __is_in_place_type with the decayed type.
(make_any): Adjust to use the new tag type.
* include/std/utility (in_place_tag): Remove.
(in_place_t): Turn into a non-reference tag type.
(__in_place, __in_place_type, __in_place_index): Remove.
(in_place): Turn into an inline variable of non-reference
tag type.
(in_place<_Tp>): Remove.
(in_place_index<_Idx>): Remove.
(in_place_type_t): New.
(in_place_type): Turn into a variable template of non-reference
type.
(in_place_index_t): New.
(in_place_index): Turn into a variable template of non-reference
type.
* include/std/variant
(_Variant_storage(in_place_index_t<_Np>, _Args&&...)): Adjust to
use the new tag type.
(_Union(in_place_index_t<0>, _Args&&...)): Likewise.
(_Union(in_place_index_t<_Np>, _Args&&...)): Likewise.
(_Variant_base()): Likewise.
(variant(_Tp&&)): Likewise.
(variant(in_place_type_t<_Tp>, _Args&&...)): Likewise.
(variant(in_place_type_t<_Tp>, initializer_list<_Up>,
_Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, _Args&&...)): Likewise.
(variant(in_place_index_t<_Np>, initializer_list<_Up>,
_Args&&...)): Likewise
(variant(allocator_arg_t, const _Alloc&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, _Tp&&)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_type_t<_Tp>,
initializer_list<_Up>, _Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
_Args&&...)): Likewise.
(variant(allocator_arg_t, const _Alloc&, in_place_index_t<_Np>,
initializer_list<_Up>, _Args&&...)): Likewise.
(emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/cons/explicit.cc: Likewise.
* testsuite/20_util/any/cons/in_place.cc: Likewise.
* testsuite/20_util/any/requirements.cc: Add tests to
check that any is not constructible from the new in_place_type_t
of any value category.
* testsuite/20_util/in_place/requirements.cc: Adjust to
use the new tag type.
* testsuite/20_util/variant/compile.cc: Likewise.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r242401
2016-11-14 21:47:44 +01:00
|
|
|
::new (this) variant(in_place_index<_Np>, __il,
|
2016-09-22 11:56:54 +02:00
|
|
|
std::forward<_Args>(__args)...);
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
__catch (...)
|
|
|
|
{
|
|
|
|
this->_M_index = variant_npos;
|
|
|
|
__throw_exception_again;
|
|
|
|
}
|
|
|
|
__glibcxx_assert(index() == _Np);
|
Implement LWG 2857, {variant,optional,any}::emplace should return the constructed value.
Implement LWG 2857, {variant,optional,any}::emplace should
return the constructed value.
* include/std/any (emplace(_Args&&...)): Change the return type and
return a reference to the constructed value.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/optional (emplace(_Args&&...)): Likewise.
(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
* include/std/variant (emplace<_Tp>(_Args&&...)): Likewise.
(emplace<_Tp>(initializer_list<_Up>, _Args&&...)): Likewise.
(emplace<_Np>(_Args&&...)): Likewise.
(emplace<_Np>(initializer_list<_Up>, _Args&&...)): Likewise.
* testsuite/20_util/any/assign/emplace.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
* testsuite/20_util/optional/assignment/6.cc: Add tests for
checking the return value of emplace.
* testsuite/20_util/variant/run.cc: Likewise.
From-SVN: r246182
2017-03-16 00:13:20 +01:00
|
|
|
return std::get<_Np>(*this);
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool valueless_by_exception() const noexcept
|
|
|
|
{ return !this->_M_valid(); }
|
|
|
|
|
|
|
|
constexpr size_t index() const noexcept
|
2017-01-11 12:23:43 +01:00
|
|
|
{
|
|
|
|
if (this->_M_index ==
|
|
|
|
typename _Base::_Storage::__index_type(variant_npos))
|
|
|
|
return variant_npos;
|
|
|
|
return this->_M_index;
|
|
|
|
}
|
2016-08-18 22:31:26 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
swap(variant& __rhs)
|
2017-01-06 16:27:01 +01:00
|
|
|
noexcept((__is_nothrow_swappable<_Types>::value && ...)
|
2016-12-06 12:26:48 +01:00
|
|
|
&& is_nothrow_move_constructible_v<variant>)
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
if (this->index() == __rhs.index())
|
|
|
|
{
|
|
|
|
if (this->_M_valid())
|
|
|
|
{
|
|
|
|
static constexpr void (*_S_vtable[])(void*, void*) =
|
2016-12-06 12:28:09 +01:00
|
|
|
{ &__detail::__variant::__erased_swap<_Types&, _Types&>... };
|
2016-08-18 22:31:26 +02:00
|
|
|
_S_vtable[__rhs._M_index](this->_M_storage(),
|
|
|
|
__rhs._M_storage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!this->_M_valid())
|
|
|
|
{
|
2016-12-06 12:26:48 +01:00
|
|
|
this->_M_destructive_move(std::move(__rhs));
|
|
|
|
__rhs._M_reset();
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
else if (!__rhs._M_valid())
|
|
|
|
{
|
2016-12-06 12:26:48 +01:00
|
|
|
__rhs._M_destructive_move(std::move(*this));
|
|
|
|
this->_M_reset();
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto __tmp = std::move(__rhs);
|
2016-12-06 12:26:48 +01:00
|
|
|
__rhs._M_destructive_move(std::move(*this));
|
|
|
|
this->_M_destructive_move(std::move(__tmp));
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 12:20:13 +01:00
|
|
|
private:
|
2017-02-15 10:01:06 +01:00
|
|
|
#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
|
|
|
|
template<size_t... __indices> \
|
|
|
|
static constexpr bool \
|
|
|
|
(*_S_erased_##__NAME[])(const variant&, const variant&) = \
|
|
|
|
{ &__detail::__variant::__erased_##__NAME< \
|
|
|
|
const variant&, __indices>... }; \
|
|
|
|
template<size_t... __indices> \
|
|
|
|
constexpr inline bool \
|
|
|
|
_M_##__NAME(const variant& __rhs, \
|
|
|
|
std::index_sequence<__indices...>) const \
|
|
|
|
{ \
|
|
|
|
auto __lhs_index = this->index(); \
|
|
|
|
auto __rhs_index = __rhs.index(); \
|
|
|
|
if (__lhs_index != __rhs_index || valueless_by_exception()) \
|
|
|
|
/* Modulo addition. */ \
|
|
|
|
return __lhs_index + 1 __OP __rhs_index + 1; \
|
|
|
|
return _S_erased_##__NAME<__indices...>[__lhs_index](*this, __rhs); \
|
2016-12-06 12:20:13 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 10:01:06 +01:00
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
|
2016-12-06 12:20:13 +01:00
|
|
|
|
2017-02-15 10:01:06 +01:00
|
|
|
#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
|
2016-12-06 12:20:13 +01:00
|
|
|
|
|
|
|
template<size_t _Np, typename _Vp>
|
|
|
|
friend constexpr decltype(auto) __detail::__variant::
|
|
|
|
#if _GLIBCXX_INLINE_VERSION
|
2017-05-10 22:40:28 +02:00
|
|
|
__8:: // Required due to PR c++/59256
|
2016-12-06 12:20:13 +01:00
|
|
|
#endif
|
|
|
|
__get(_Vp&& __v);
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
template<typename _Vp>
|
2016-10-03 16:35:28 +02:00
|
|
|
friend void* __detail::__variant::
|
|
|
|
#if _GLIBCXX_INLINE_VERSION
|
2017-05-10 22:40:28 +02:00
|
|
|
__8:: // Required due to PR c++/59256
|
2016-10-03 16:35:28 +02:00
|
|
|
#endif
|
|
|
|
__get_storage(_Vp&& __v);
|
2016-12-06 12:20:13 +01:00
|
|
|
|
2017-02-15 10:01:06 +01:00
|
|
|
#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
|
|
|
|
template<typename... _Tp> \
|
|
|
|
friend constexpr bool \
|
|
|
|
operator __OP(const variant<_Tp...>& __lhs, \
|
|
|
|
const variant<_Tp...>& __rhs);
|
|
|
|
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(<)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(==)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
|
|
|
|
_VARIANT_RELATION_FUNCTION_TEMPLATE(>)
|
2016-12-06 12:20:13 +01:00
|
|
|
|
2017-02-15 10:01:06 +01:00
|
|
|
#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
|
2016-08-18 22:31:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr variant_alternative_t<_Np, variant<_Types...>>&
|
2016-08-18 22:31:26 +02:00
|
|
|
get(variant<_Types...>& __v)
|
|
|
|
{
|
|
|
|
static_assert(_Np < sizeof...(_Types),
|
|
|
|
"The index should be in [0, number of alternatives)");
|
|
|
|
if (__v.index() != _Np)
|
|
|
|
__throw_bad_variant_access("Unexpected index");
|
2016-12-06 12:20:13 +01:00
|
|
|
return __detail::__variant::__get<_Np>(__v);
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr variant_alternative_t<_Np, variant<_Types...>>&&
|
2016-08-18 22:31:26 +02:00
|
|
|
get(variant<_Types...>&& __v)
|
|
|
|
{
|
|
|
|
static_assert(_Np < sizeof...(_Types),
|
|
|
|
"The index should be in [0, number of alternatives)");
|
|
|
|
if (__v.index() != _Np)
|
|
|
|
__throw_bad_variant_access("Unexpected index");
|
2016-12-06 12:20:13 +01:00
|
|
|
return __detail::__variant::__get<_Np>(std::move(__v));
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr const variant_alternative_t<_Np, variant<_Types...>>&
|
2016-08-18 22:31:26 +02:00
|
|
|
get(const variant<_Types...>& __v)
|
|
|
|
{
|
|
|
|
static_assert(_Np < sizeof...(_Types),
|
|
|
|
"The index should be in [0, number of alternatives)");
|
|
|
|
if (__v.index() != _Np)
|
|
|
|
__throw_bad_variant_access("Unexpected index");
|
2016-12-06 12:20:13 +01:00
|
|
|
return __detail::__variant::__get<_Np>(__v);
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t _Np, typename... _Types>
|
2016-12-06 12:20:13 +01:00
|
|
|
constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
|
2016-08-18 22:31:26 +02:00
|
|
|
get(const variant<_Types...>&& __v)
|
|
|
|
{
|
|
|
|
static_assert(_Np < sizeof...(_Types),
|
|
|
|
"The index should be in [0, number of alternatives)");
|
|
|
|
if (__v.index() != _Np)
|
|
|
|
__throw_bad_variant_access("Unexpected index");
|
2016-12-06 12:20:13 +01:00
|
|
|
return __detail::__variant::__get<_Np>(std::move(__v));
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _Visitor, typename... _Variants>
|
2016-12-06 12:28:09 +01:00
|
|
|
constexpr decltype(auto)
|
2016-08-18 22:31:26 +02:00
|
|
|
visit(_Visitor&& __visitor, _Variants&&... __variants)
|
|
|
|
{
|
2016-12-06 12:28:09 +01:00
|
|
|
if ((__variants.valueless_by_exception() || ...))
|
|
|
|
__throw_bad_variant_access("Unexpected index");
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
using _Result_type =
|
2017-02-15 08:38:20 +01:00
|
|
|
decltype(std::forward<_Visitor>(__visitor)(
|
|
|
|
get<0>(std::forward<_Variants>(__variants))...));
|
2016-12-06 12:28:09 +01:00
|
|
|
|
|
|
|
constexpr auto& __vtable = __detail::__variant::__gen_vtable<
|
|
|
|
_Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
|
|
|
|
|
|
|
|
auto __func_ptr = __vtable._M_access(__variants.index()...);
|
2016-09-22 11:56:54 +02:00
|
|
|
return (*__func_ptr)(std::forward<_Visitor>(__visitor),
|
2016-12-06 12:28:09 +01:00
|
|
|
std::forward<_Variants>(__variants)...);
|
2016-08-18 22:31:26 +02:00
|
|
|
}
|
|
|
|
|
2017-01-21 16:38:23 +01:00
|
|
|
template<bool, typename... _Types>
|
|
|
|
struct __variant_hash_call_base_impl
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const variant<_Types...>& __t) const
|
P0604R0 add invoke_result, is_invocable etc. for C++17
* include/bits/invoke.h (__invoke): Use __invoke_result instead of
result_of, and __is_nothrow_invocable instead of
__is_nothrow_callable.
* include/bits/shared_ptr_base.h (__shared_ptr): Use __is_invocable
instead of __is_callable.
* include/std/functional (invoke): use invoke_result_t instead of
result_of_t and is_nothrow_invocable instead of is_nothrow_callable.
(_Not_fn): Use __invoke_result instead of result_of.
* include/std/type_traits (__result_of_memobj, __result_of_memfun):
Remove partial specializations for reference_wrapper types.
(__result_of_impl): Use __inv_unwrap to strip reference_wrapper.
(__invoke_result): Define replacement for result_of and then use it to
define result_of.
(__is_callable_impl, __is_callable, __is_nothrow_callable): Replace
with __is_invocable_impl, __is_invocable, and __is_nothrow_invocable
respectively.
(invoke_result, invoke_result_t): Define for C++17.
(is_callable, is_nothrow_callable): Replace with is_invocable,
is_invocable_r, is_nothrow_invocable, and is_nothrow_invocable_r.
(is_callable_v, is_nothrow_callable_v): Replace with is_invocable_v,
is_invocable_r_v, is_nothrow_invocable_v, and is_nothrow_invocable_r_v.
* include/std/variant (hash<variant<T...>>): Use is_nothrow_invocable_v
instead of is_nothrow_callable_v.
* testsuite/20_util/function_objects/invoke/59768.cc: Remove unused
main function.
* testsuite/20_util/function_objects/not_fn/1.cc: Use is_invocable
instead of is_callable.
* testsuite/20_util/is_callable/*: Rename directory and adjust tests
to use new traits.
* testsuite/20_util/is_notjrow_callable/*: Likewise.
* testsuite/20_util/optional/hash.cc: Use is_invocable_v instead of
is_callable.
* testsuite/20_util/variant/hash.cc: Likewise.
From-SVN: r246036
2017-03-10 16:29:38 +01:00
|
|
|
noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
|
2016-08-18 22:31:26 +02:00
|
|
|
{
|
|
|
|
if (!__t.valueless_by_exception())
|
|
|
|
{
|
|
|
|
namespace __edv = __detail::__variant;
|
|
|
|
static constexpr size_t (*_S_vtable[])(void*) =
|
2016-12-06 12:28:09 +01:00
|
|
|
{ &__edv::__erased_hash<const _Types&>... };
|
2016-08-18 22:31:26 +02:00
|
|
|
return hash<size_t>{}(__t.index())
|
|
|
|
+ _S_vtable[__t.index()](__edv::__get_storage(__t));
|
|
|
|
}
|
|
|
|
return hash<size_t>{}(__t.index());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-21 16:38:23 +01:00
|
|
|
template<typename... _Types>
|
|
|
|
struct __variant_hash_call_base_impl<false, _Types...> {};
|
|
|
|
|
|
|
|
template<typename... _Types>
|
|
|
|
using __variant_hash_call_base =
|
|
|
|
__variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
|
|
|
|
__enable_hash_call &&...), _Types...>;
|
|
|
|
|
|
|
|
template<typename... _Types>
|
|
|
|
struct hash<variant<_Types...>>
|
|
|
|
: private __detail::__variant::_Variant_hash_base<
|
|
|
|
variant<_Types...>, std::index_sequence_for<_Types...>>,
|
|
|
|
public __variant_hash_call_base<_Types...>
|
|
|
|
{
|
|
|
|
using result_type = size_t;
|
|
|
|
using argument_type = variant<_Types...>;
|
|
|
|
};
|
|
|
|
|
2016-08-18 22:31:26 +02:00
|
|
|
template<>
|
|
|
|
struct hash<monostate>
|
|
|
|
{
|
|
|
|
using result_type = size_t;
|
|
|
|
using argument_type = monostate;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
operator()(const monostate& __t) const noexcept
|
|
|
|
{
|
|
|
|
constexpr size_t __magic_monostate_hash = -7777;
|
|
|
|
return __magic_monostate_hash;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
#endif // C++17
|
|
|
|
|
|
|
|
#endif // _GLIBCXX_VARIANT
|