Commit Graph

30 Commits

Author SHA1 Message Date
Jakub Jelinek 8d9254fc8a Update copyright years.
From-SVN: r279813
2020-01-01 12:51:42 +01:00
Jonathan Wakely ac781bc880 Fix std::variant test for ILP32 targets
* testsuite/20_util/variant/compile.cc: Fix narrowing test for ILP32
	targets. Add more cases from P0608R3.
	* testsuite/20_util/variant/run.cc: Add more cases from P0608R3.

From-SVN: r271325
2019-05-17 15:13:43 +01:00
Jonathan Wakely d069df01ed Implement sane variant converting constructor (P0608R3)
* include/std/variant (__overload_set): Remove.
	(_Arr): New helper.
	(_Build_FUN): New class template to define a single FUN overload,
	with specializations to prevent unwanted conversions, as per P0608R3.
	(_Build_FUNs): New class template to build an overload set of FUN.
	(_FUN_type): New alias template to perform overload resolution.
	(__accepted_type): Use integer_constant base for failure case. Use
	_FUN_type for successful case.
	(variant::__accepted_index): Use _Tp instead of _Tp&&.
	(variant::variant(_Tp&&)): Likewise.
	(variant::operator=(_Tp&&)): Likewise.

From-SVN: r271296
2019-05-16 21:30:35 +01:00
Jonathan Wakely 038bc9bfd6 Implement correct std::variant triviality rules from P0602R4
The std::variant move assignment operator should not be trivial if the
variant is not trivially move constructible.

	* include/std/variant (__detail::__variant::_Traits): Make
	_S_trivial_copy_assign depend on _S_trivial_copy_ctor and make
	_S_trivial_move_assign depend on _S_trivial_move_ctor, as per
	P0602R4.
	(__detail::__variant::_Copy_assign_alias): Only depend on
	_S_trivial_copy_assign, which subsumes _S_trivial_copy_ctor now.
	* testsuite/20_util/variant/compile.cc: Correct checks for trivial
	move assignment operators.

From-SVN: r270510
2019-04-23 13:48:28 +01:00
Jonathan Wakely 06715e1cfb PR libstdc++/90165 constrain variant(T&&) constructor
Also refactor some constraints slightly to be more readable.

	PR libstdc++/90165
	* include/std/variant (variant::__not_self): New helper for the
	is_same_v<remove_cvref_t<T>, variant>==false constraints.
	(variant::__to_type_impl): Remove.
	(variant::__to_type): Add default argument to check pack size, instead
	of using __to_type_impl.
	(variant::__accepted_type): Add default argument using __not_self.
	(variant::__is_in_place_tag, variant::__not_in_place_tag): New helpers
	for variant(T&&) constructor constraint.
	(variant::variant(T&&)): Use __not_in_place_tag in constraints.
	Extract __accepted_type into a named template parameter for reuse in
	other constraints and in the exception specification.
	(variant::variant(in_place_type_t<T>, Args&&...))
	(variant::variant(in_place_type_t<T>, initializer_list<U>, Args&&...))
	(variant::variant(in_place_index_t<T>, Args&&...))
	(variant::variant(in_place_index_t<T>, initializer_list<U>, Args&&...))
	(variant::operator=T&&)): Remove redundant && from trait arguments.
	* testsuite/20_util/variant/compile.cc: Check variant(T&&) constructor
	isn't used for in_place_type or in_place_index arguments.

From-SVN: r270509
2019-04-23 13:48:18 +01:00
Jonathan Wakely 47a468bdbe Fix std::variant regression caused by never-valueless optimization
A regression was introduced by the recent changes to provide the strong
exception safety guarantee for "never valueless" types that have O(1),
non-throwing move assignment. The problematic code is:

  else if constexpr (__detail::__variant::_Never_valueless_alt<type>())
    {
      // This construction might throw:
      variant __tmp(in_place_index<_Np>, __il,
                    std::forward<_Args>(__args)...);
      // But _Never_valueless_alt<type> means this won't:
      *this = std::move(__tmp);
    }

When the variant is not assignable, the assignment is ill-formed, so
should not be attempted. When the variant has a copy assignment operator
but not a move assignment operator, the assignment performs a copy
assignment and that could throw, so should not be attempted.

The solution is to only take that branch when the variant has a move
assignment operator, which is determined by the _Traits::_S_move_assign
constant. When that is false the strong exception safety guarantee is
not possible, and so the __never_valueless function should also depend
on _S_move_assign.

While testing the fixes for this I noticed that the partial
specialization _Never_valueless_alt<basic_string<C,T,A>> incorrectly
assumed that is_nothrow_move_constructible<basic_string<C,T,A>> is
always true, but that's wrong for fully-dynamic COW strings. Fix the
partial specialization, and improve the comment describing
_Never_valueless_alt to be clear it depends on move construction as well
as move assignment.

Finally, I also observed that _Variant_storage<false, T...>::_M_valid()
was not taking advantage of the __never_valueless<T...>() function to
avoid a runtime check. Only the _Variant_storage<true, T...>::_M_valid()
function was using __never_valueless. That is also fixed.

	PR libstdc++/87431
	* include/bits/basic_string.h (_Never_valueless_alt): Make partial
	specialization also depend on is_nothrow_move_constructible.
	* include/std/variant (__detail::__variant::__never_valueless()):
	Only true if the variant would have a move assignment operator.
	(__detail::__variant::_Variant_storage<false, T...>::_M_valid()):
	Check __never_valueless<T...>().
	(variant::emplace): Only perform non-throwing move assignments
	for never-valueless alternatives if the variant has a move assignment
	operator.
	* testsuite/20_util/variant/compile.cc: Check that never-valueless
	types can be emplaced into non-assignable variants.
	* testsuite/20_util/variant/run.cc: Check that never-valueless types
	don't get copied when emplaced into non-assignable variants.

From-SVN: r270502
2019-04-23 10:55:33 +01:00
Jonathan Wakely 5f00d0d5c2 Fix condition for std::variant to be copy constructible
The standard says the std::variant copy constructor is defined as
deleted unless all alternative types are copy constructible, but we were
making it also depend on move constructible. Fix the condition and
enhance the tests to check the semantics with pathological copy-only
types (i.e. supporting copying but having deleted moves).

The enhanced tests revealed a regression in copy assignment for
non-trivial alternative types, where the assignment would not be
performed because the condition in the _Copy_assign_base visitor is
false: is_same_v<remove_reference_t<T&>, remove_reference_t<const T&>>.

	* include/std/variant (__detail::__variant::_Traits::_S_copy_assign):
	Do not depend on whether all alternative types are move constructible.
	(__detail::__variant::_Copy_assign_base::operator=): Remove cv-quals
	from the operand when deciding whether to perform the assignment.
	* testsuite/20_util/variant/compile.cc (DeletedMoves): Define type
	with deleted move constructor and deleted move assignment operator.
	(default_ctor, copy_ctor, move_ctor, copy_assign, move_assign): Check
	behaviour of variants with DeletedMoves as an alternative.
	* testsuite/20_util/variant/run.cc (DeletedMoves): Define same type.
	(move_ctor, move_assign): Check that moving a variant with a
	DeletedMoves alternative falls back to copying instead of moving.

From-SVN: r270425
2019-04-17 20:27:27 +01:00
Jonathan Wakely 990666d05a Remove unnecessary string literals from static_assert in C++17 tests
The string literal is optional in C++17 and all these are empty so add
no value.

	* testsuite/20_util/variant/compile.cc: Remove empty string literals
	from static_assert declarations.

From-SVN: r270424
2019-04-17 20:27:23 +01:00
Jonathan Wakely 9d3e662d29 Fix tests for std::variant to match original intention
* testsuite/20_util/variant/compile.cc (MoveCtorOnly): Fix type to
	actually match its name.
	(MoveCtorAndSwapOnly): Define new type that adds swap to MoveCtorOnly.
	(test_swap()): Fix result for MoveCtorOnly and check
	MoveCtorAndSwapOnly.

From-SVN: r270423
2019-04-17 20:27:19 +01:00
Ville Voutilainen 669a6fdcb4 Rewrite variant, also PR libstdc++/85517
* include/std/variant (__do_visit): New.
(__variant_cast): Likewise.
(__variant_cookie): Likewise.
(__erased_*): Remove.
(_Variant_storage::_S_vtable): Likewise.
(_Variant_storage::__M_reset_impl): Adjust to use __do_visit.
(_Variant_storage::__M_reset): Adjust.
(__variant_construct): New.
(_Copy_ctor_base(const _Copy_ctor_base&)): Adjust to use
__variant_construct.
(_Move_ctor_base(_Move_ctor_base&&)): Likewise.
(_Move_ctor_base::__M_destructive_copy): New.
(_Move_ctor_base::__M_destructive_move): Adjust to use
__variant_construct.
(_Copy_assign_base::operator=): Adjust to use __do_visit.
(_Copy_assign_alias): Adjust to check both copy assignment
and copy construction for triviality.
(_Move_assign_base::operator=): Adjust to use __do_visit.
(_Multi_array): Add support for visitors that accept and return
a __variant_cookie.
(__gen_vtable_impl::_S_apply_all_alts): Likewise.
(__gen_vtable_impl::_S_apply_single_alt): Likewise.
(__gen_vtable_impl::__element_by_index_or_cookie): New. Generate
a __variant_cookie temporary for a variant that is valueless and..
(__gen_vtable_impl::__visit_invoke): ..adjust here.
(__gen_vtable::_Array_type): Conditionally make space for
the __variant_cookie visitor case.
(__variant_construct_by_index): New.
(get_if): Adjust to use std::addressof.
(relops): Adjust to use __do_visit.
(variant): Add __variant_cast and __variant_construct_by_index
as friends.
(variant::emplace): Use _M_reset() and __variant_construct_by_index
instead of self-destruction.
(variant::swap): Adjust to use __do_visit.
(visit): Reimplement in terms of __do_visit.
(__variant_hash_call_base_impl::operator()): Adjust to use __do_visit.
* testsuite/20_util/variant/compile.cc: Adjust.
* testsuite/20_util/variant/run.cc: Likewise.

From-SVN: r269422
2019-03-06 14:56:05 +02:00
Jakub Jelinek a554497024 Update copyright years.
From-SVN: r267494
2019-01-01 13:31:55 +01:00
Jakub Jelinek 85ec4feb11 Update copyright years.
From-SVN: r256169
2018-01-03 11:03:58 +01:00
Paolo Carlini 98910bc2b9 deduction.cc: Avoid -Wreturn-type warnings.
2017-11-06  Paolo Carlini  <paolo.carlini@oracle.com>

	* testsuite/20_util/optional/cons/deduction.cc: Avoid -Wreturn-type
	warnings.
	* testsuite/20_util/pair/cons/deduction.cc: Likewise.
	* testsuite/20_util/pair/traits.cc: Likewise.
	* testsuite/20_util/tuple/cons/deduction.cc: Likewise.
	* testsuite/20_util/variant/compile.cc: Likewise.
	* testsuite/23_containers/map/modifiers/try_emplace/1.cc: Likewise.
	* testsuite/23_containers/unordered_map/modifiers/try_emplace.cc:
	Likewise.

From-SVN: r254450
2017-11-06 12:55:35 +00:00
Tim Shen 7050372474 re PR libstdc++/80187 (C++ variant should be trivially copy constructible if possible)
PR libstdc++/80187
	* include/std/variant (variant::variant, variant::~variant,
	variant::operator=): Implement triviality forwarding for four
	special member functions.
	* testsuite/20_util/variant/compile.cc: Tests.

From-SVN: r249706
2017-06-27 18:19:03 +00:00
Tim Shen d255829bbc re PR libstdc++/78723 ([variant] P0393r3: "Making variant greater equal again" is unimplemented)
PR libstdc++/78723
	* include/std/variant (operator<(), operator>(), operator<=(),
	operator>=(), operator==(), operator!=()): Implement P0393R3.
	* testsuite/20_util/variant/compile.cc: Adjust tests.
	* testsuite/20_util/variant/run.cc: Adjust tests.

From-SVN: r245475
2017-02-15 09:01:06 +00:00
Tim Shen 7d5abe42c2 re PR libstdc++/79513 (std::visit doesn't handle references)
PR libstdc++/79513
	* include/std/variant (visit()): Forward variant types to the return
	type detection code.
	* testsuite/20_util/variant/compile.cc: Add test cases.

From-SVN: r245474
2017-02-15 07:38:20 +00:00
Jakub Jelinek cbe34bb5ed Update copyright years.
From-SVN: r243994
2017-01-01 13:07:43 +01:00
Tim Shen b01af236b7 variant (visit): Make visit constexpr.
* include/std/variant (visit): Make visit constexpr. Also cleanup
	__get_alternative and __storage, since we don't support reference/void
	alternatives any more.
	* testsuite/20_util/variant/compile.cc: Add tests.

From-SVN: r243295
2016-12-06 11:28:09 +00:00
Tim Shen 458ef69052 enable_special_members.h: Make _Enable_default_constructor constexpr.
* include/bits/enable_special_members.h: Make
	_Enable_default_constructor constexpr.
	* include/std/variant (variant::emplace, variant::swap, std::swap,
	std::hash): Sfinae on emplace and std::swap; handle __poison_hash bases
	of duplicated types.
	* testsuite/20_util/variant/compile.cc: Add tests.
	* testsuite/20_util/variant/hash.cc: Add tests.

From-SVN: r243294
2016-12-06 11:26:48 +00:00
Tim Shen 9189f55908 variant (std::get, operator==): Implement constexpr comparison and get<>.
* include/std/variant (std::get, operator==): Implement constexpr
	comparison and get<>.
	* testsuite/20_util/variant/compile.cc: Tests.

From-SVN: r243293
2016-12-06 11:20:13 +00:00
Tim Shen 44f4688595 variant (__erased_use_alloc_ctor, [...]): Remove uses-allocator related functions.
* include/std/variant (__erased_use_alloc_ctor,
	_Variant_base::_Variant_base, variant::variant): Remove uses-allocator
	related functions.
	* testsuite/20_util/variant/compile.cc: Remove related tests.
	* testsuite/20_util/variant/run.cc: Remove related tests.

From-SVN: r243292
2016-12-06 11:17:56 +00:00
Ville Voutilainen a2863bde75 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 18:23:21 +02:00
Tim Shen 3203ed5f01 re PR libstdc++/78441 ([variant] variant_alternative doesn't allow cv qualifiers)
PR libstdc++/78441
	* include/std/variant: Propagate cv qualifications to types returned
	by variant_alternative.
	* testsuite/20_util/variant/compile.cc: Tests.

From-SVN: r242892
2016-11-27 00:32:04 +00:00
Tim Shen 7b277e8b46 variant: Remove variant<T&>...
* include/std/variant: Remove variant<T&>, variant<void>, variant<> support
	to rebase on the post-Issaquah design.
	* testsuite/20_util/variant/compile.cc: Likewise.

From-SVN: r242437
2016-11-15 17:26:59 +00:00
Ville Voutilainen 627a2f5923 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 22:47:44 +02:00
Jason Merrill 36cbfdb066 Further P0135 refinement.
* call.c (build_user_type_conversion_1): Consider conversions from
	a single element in an initializer-list.
	(build_temp): Undo early_elide_copy change.
	(build_over_call): Check that we don't try to copy a TARGET_EXPR
	in C++17 mode.  Set user_conv_p here.
	(convert_like_real): Not here.
	(check_self_delegation): Split out from...
	(build_special_member_call): ...here.  Handle C++17 copy elision.
	* cvt.c (early_elide_copy): Remove.
	(ocp_convert): Undo early_elide_copy change.
	* except.c (build_throw): Likewise.
	* init.c (expand_default_init): Likewise.
	* typeck.c (cp_build_modify_expr): Likewise.

From-SVN: r240889
2016-10-08 12:23:26 -04:00
Jonathan Wakely 64626fcaaa Always qualify std::forward in <variant>
* include/bits/uses_allocator.h (__uses_allocator_construct): Qualify
	std::forward and ::new. Cast pointer to void*.
	* include/std/variant (_Variant_storage, _Union, _Variant_base)
	(__access, __visit_invoke, variant, visit): Qualify std::forward.
	* testsuite/20_util/variant/compile.cc: Test for ADL problems.

From-SVN: r240344
2016-09-22 10:56:54 +01:00
Tim Shen c42bc5d73b variant (variant::operator=): Fix assignment on references.
* libstdc++-v3/include/std/variant (variant::operator=): Fix assignment
	on references.
	* libstdc++-v3/testsuite/20_util/variant/compile.cc: Add test.

From-SVN: r240343
2016-09-22 08:45:55 +00:00
Tim Shen 41501d1ae7 re PR libstdc++/77641 (std::variant copy-initialization fails for type with non-trivial constexpr ctor)
PR libstdc++/77641
	* include/std/variant (_Variant_storage::_Variant_storage):
	Change _Variant_storage's union to be default constructible.
	* testsuite/20_util/variant/compile.cc: New test.

From-SVN: r240340
2016-09-22 03:15:58 +00:00
Tim Shen 197c757cb1 Implement <variant>
* include/Makefile.am: Add new file std/variant.
	* include/Makefile.in: Generated from Makefile.am.
	* include/bits/enable_special_members.h: Add a tag type to allow
	the construction in non-default constructor.
	* include/bits/uses_allocator.h: Add convenience traits to
	detect constructibility.
	* include/std/variant: Implement <variant>.
	* testsuite/20_util/variant/compile.cc: Compile-time tests.
	* testsuite/20_util/variant/run.cc: Runtime tests.

From-SVN: r239590
2016-08-18 20:31:26 +00:00