gcc/libstdc++-v3/include/std
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
..
algorithm Integrate C++17 parallel algorithms 2019-03-21 23:48:49 +00:00
any PR libstdc++/87431 re-adjust never-valueless optimizations 2019-04-05 17:56:09 +01:00
array Fix after P0600. 2019-01-21 11:47:30 +00:00
atomic re PR libstdc++/89641 (std::atomic<T> no longer works) 2019-03-11 12:49:13 +01:00
bit
bitset
charconv P0482R5 char8_t: Standard library support 2019-02-19 02:54:42 +00:00
chrono P0972R0 <chrono> zero(), min(), and max() should be noexcept 2019-01-11 23:41:05 +00:00
codecvt
complex
condition_variable
deque Define __cpp_lib_erase_if feature test macro 2019-01-10 13:49:31 +00:00
execution Integrate C++17 parallel algorithms 2019-03-21 23:48:49 +00:00
filesystem
forward_list Avoid ambiguity between C++2a std::erase_if and LFTS version 2019-01-29 00:47:38 +00:00
fstream
functional P0356R5 Simplified partial function application 2019-03-07 14:15:53 +00:00
future Fix after P0600. 2019-01-21 11:47:30 +00:00
iomanip
ios
iosfwd
iostream
istream
iterator Fix more missing or incorrect feature test macros 2019-01-15 12:58:19 +00:00
limits P0482R5 char8_t: Standard library support 2019-02-19 02:54:42 +00:00
list Avoid ambiguity between C++2a std::erase_if and LFTS version 2019-01-29 00:47:38 +00:00
locale
map
memory Integrate C++17 parallel algorithms 2019-03-21 23:48:49 +00:00
memory_resource Implement polymorphic_allocator<byte> for C++20 (P0339R6) 2019-03-04 12:21:06 +00:00
mutex
numeric Integrate C++17 parallel algorithms 2019-03-21 23:48:49 +00:00
optional Fix missing or incorrect feature test macros 2019-01-15 12:01:12 +00:00
ostream Implement LWG 2221: formatted output operator for nullptr 2019-01-10 13:14:57 +00:00
queue
random
ratio
regex
scoped_allocator Fix after P0600. 2019-01-21 11:47:30 +00:00
set
shared_mutex
sstream
stack
stdexcept
streambuf
string Define std::hash specializations for C++17 PMR strings 2019-04-03 10:47:47 +01:00
string_view P0482R5 char8_t: Standard library support 2019-02-19 02:54:42 +00:00
system_error
thread
tuple
type_traits Add feature test macro for bounded array traits 2019-03-06 15:31:06 +00:00
typeindex
unordered_map
unordered_set
utility
valarray
variant PR libstdc++/87431 re-adjust never-valueless optimizations 2019-04-05 17:56:09 +01:00
vector Ensure pool resources always use normal mode vector 2019-01-29 00:47:25 +00:00
version Integrate C++17 parallel algorithms 2019-03-21 23:48:49 +00:00