gcc/libstdc++-v3/include/debug
Jonathan Wakely 10f26de915 PR libstdc++/87431 re-adjust never-valueless optimizations
Avoid creating arbitrarily large objects on the stack when emplacing
trivially copyable objects into a variant. Currently we provide the
strong exception-safety guarantee for all trivially copyable types, by
constructing a second variant and then doing a non-throwing move
assignment from the temporary. This patch restricts that behaviour to
trivially copyable types that are no larger than 256 bytes. For larger
types the object will be emplaced directly into the variant, and if its
initialization throws then the variant becomes valueless.

Also implement Antony Polukhin's suggestion to whitelist specific types
that are not trivially copyable but can be efficiently move-assigned.
Emplacing those types will never cause a variant to become valueless.
The whitelisted types are: std::shared_ptr, std::weak_ptr,
std::unique_ptr, std::function, and std::any. Additionally,
std::basic_string, std::vector, and __gnu_debug::vector are whitelisted
if their allocator traits give them a non-throwing move assignment
operator. Specifically, this means std::string is whitelisted, but
std::pmr::string is not.

As part of this patch, additional if-constexpr branches are added for
the cases where the initialization is known to be non-throwing (so the
overhead of the try-catch block can be avoided) and where a scalar is
being produced by a potentially-throwing conversion operator (so that
the overhead of constructing and move-assigning a variant is avoided).
These changes should have no semantic effect, just better codegen.

	PR libstdc++/87431 (again)
	* include/bits/basic_string.h (__variant::_Never_valueless_alt):
	Define partial specialization for basic_string.
	* include/bits/shared_ptr.h (_Never_valueless_alt): Likewise for
	shared_ptr and weak_ptr.
	* include/bits/std_function.h (_Never_valueless_alt): Likewise for
	function.
	* include/bits/stl_vector.h (_Never_valueless_alt): Likewise for
	vector.
	* include/bits/unique_ptr.h (_Never_valueless_alt): Likewise for
	unique_ptr.
	* include/debug/vector (_Never_valueless_alt): Likewise for debug
	vector.
	* include/std/any (_Never_valueless_alt): Define explicit
	specialization for any.
	* include/std/variant (_Never_valueless_alt): Define primary template.
	(__never_valueless): Use _Never_valueless_alt instead of
	is_trivially_copyable.
	(variant::emplace<N>(Args&&...)): Add special case for non-throwing
	initializations to avoid try-catch overhead. Add special case for
	scalars produced by potentially-throwing conversions. Use
	_Never_valueless_alt instead of is_trivially_copyable for the
	remaining strong exception-safety cases.
	(variant::emplace<N>(initializer_list<U>, Args&&...)): Likewise.
	* testsuite/20_util/variant/87431.cc: Run both test functions.
	* testsuite/20_util/variant/exception_safety.cc: New test.
	* testsuite/20_util/variant/run.cc: Use pmr::string instead of string,
	so the variant becomes valueless.

From-SVN: r270170
2019-04-05 17:56:09 +01:00
..
array Fix after P0600. 2019-01-21 11:47:30 +00:00
assertions.h Update copyright years. 2019-01-01 13:31:55 +01:00
bitset Update copyright years. 2019-01-01 13:31:55 +01:00
debug.h Update copyright years. 2019-01-01 13:31:55 +01:00
deque Update copyright years. 2019-01-01 13:31:55 +01:00
formatter.h Update copyright years. 2019-01-01 13:31:55 +01:00
forward_list PR libstdc++/88607 replace or remove unnecessary UTF-8 characters 2019-01-03 20:38:11 +00:00
functions.h Update copyright years. 2019-01-01 13:31:55 +01:00
helper_functions.h Update copyright years. 2019-01-01 13:31:55 +01:00
list Update copyright years. 2019-01-01 13:31:55 +01:00
macros.h Update copyright years. 2019-01-01 13:31:55 +01:00
map Update copyright years. 2019-01-01 13:31:55 +01:00
map.h re PR libstdc++/89477 (Incorrect CTAD deduction guides for set and multiset) 2019-03-08 05:53:09 +00:00
multimap.h re PR libstdc++/89477 (Incorrect CTAD deduction guides for set and multiset) 2019-03-08 05:53:09 +00:00
multiset.h re PR libstdc++/89477 (Incorrect CTAD deduction guides for set and multiset) 2019-03-08 05:53:09 +00:00
safe_base.h Update copyright years. 2019-01-01 13:31:55 +01:00
safe_container.h Update copyright years. 2019-01-01 13:31:55 +01:00
safe_iterator.h Update copyright years. 2019-01-01 13:31:55 +01:00
safe_iterator.tcc Update copyright years. 2019-01-01 13:31:55 +01:00
safe_local_iterator.h Update copyright years. 2019-01-01 13:31:55 +01:00
safe_local_iterator.tcc Update copyright years. 2019-01-01 13:31:55 +01:00
safe_sequence.h Update copyright years. 2019-01-01 13:31:55 +01:00
safe_sequence.tcc Update copyright years. 2019-01-01 13:31:55 +01:00
safe_unordered_base.h Update copyright years. 2019-01-01 13:31:55 +01:00
safe_unordered_container.h Update copyright years. 2019-01-01 13:31:55 +01:00
safe_unordered_container.tcc Update copyright years. 2019-01-01 13:31:55 +01:00
set Update copyright years. 2019-01-01 13:31:55 +01:00
set.h re PR libstdc++/89477 (Incorrect CTAD deduction guides for set and multiset) 2019-03-08 05:53:09 +00:00
stl_iterator.h Update copyright years. 2019-01-01 13:31:55 +01:00
string Update copyright years. 2019-01-01 13:31:55 +01:00
unordered_map re PR libstdc++/89477 (Incorrect CTAD deduction guides for set and multiset) 2019-03-08 05:53:09 +00:00
unordered_set re PR libstdc++/89477 (Incorrect CTAD deduction guides for set and multiset) 2019-03-08 05:53:09 +00:00
vector PR libstdc++/87431 re-adjust never-valueless optimizations 2019-04-05 17:56:09 +01:00