[PATCH] PR libstdc++/48101 improve errors for invalid container specializations

Jonathan Wakely jwakely@redhat.com
Tue Nov 21 20:47:00 GMT 2017


This uses static_assert to improve the errors when attempting to
instantiate invalid specializations of containers, e.g. set<const T>,
or unordered_set<int, equal_to<int>, hash<int>> (which mixes up the
order of the hasher and equality predicate arguments).

This means instead of more than 100 lines of confusing errors for
https://wandbox.org/permlink/kL1YVNVOzrAsLPyS we get only this:

In file included from /home/jwakely/gcc/8/include/c++/8.0.0/set:61:0,
                 from s.cc:2:
/home/jwakely/gcc/8/include/c++/8.0.0/bits/stl_set.h: In instantiation of ‘class std::set<const int>’:
s.cc:8:18:   required from here
/home/jwakely/gcc/8/include/c++/8.0.0/bits/stl_set.h:108:7: error: static assertion failed: std::set must have a non-const, non-volatile value_type
       static_assert(is_same<typename remove_cv<_Key>::type, _Key>::value,
       ^~~~~~~~~~~~~
In file included from /home/jwakely/gcc/8/include/c++/8.0.0/unordered_set:46:0,
                 from s.cc:1:
/home/jwakely/gcc/8/include/c++/8.0.0/bits/hashtable.h: In instantiation of ‘class std::_Hashtable<int, int, std::allocator<int>, std::__detail::_Identity, std::hash<int>, std::equal_to<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, true, false> >’:
/home/jwakely/gcc/8/include/c++/8.0.0/bits/unordered_set.h:898:18:   required from ‘class std::unordered_multiset<int, std::equal_to<int>, std::hash<int> >’
s.cc:10:53:   required from here
/home/jwakely/gcc/8/include/c++/8.0.0/bits/hashtable.h:195:7: error: static assertion failed: hash function must be invocable with an argument of key type
       static_assert(__is_invocable<const _H1&, const _Key&>{},
       ^~~~~~~~~~~~~
/home/jwakely/gcc/8/include/c++/8.0.0/bits/hashtable.h:197:7: error: static assertion failed: key equality predicate must be invocable with two arguments of key type
       static_assert(__is_invocable<const _Equal&, const _Key&, const _Key&>{},
       ^~~~~~~~~~~~~

Tested powerpc64le-linux, committed to trunk.


	PR libstdc++/48101
	* include/bits/allocator.h (allocator<const _Tp>)
	(allocator<volatile _Tp>, allocator<const volatile _Tp>): Add partial
	specializations.
	* include/bits/forward_list.h (forward_list): Add static assertions.
	* include/bits/hashtable.h (__cache_default): Use
	__is_nothrow_invocable instead of __is_noexcept_hash.
	(_Hashtable): Add static assertions.
	* include/bits/hashtable_policy.h (__is_noexcept_hash): Remove.
	* include/bits/stl_deque.h (deque): Add static assertions.
	* include/bits/stl_function.h (_Identity<const _Tp>): Add partial
	specialization.
	* include/bits/stl_list.h (list): Add static assertions.
	* include/bits/stl_map.h (map): Likewise.
	* include/bits/stl_multimap.h (multimap): Likewise.
	* include/bits/stl_multiset.h (multiset): Likewise.
	* include/bits/stl_set.h (set): Likewise.
	* include/bits/stl_tree.h (_Rb_tree): Likewise.
	* include/bits/stl_vector.h (vector): Likewise.
	* include/bits/unordered_map.h (unordered_map, unordered_multimap):
	Use typename instead of class in template-parameter-list and remove
	spaces.
	* include/bits/unordered_set.h (unordered_set, unordered_multiset):
	Likewise.
	* testsuite/23_containers/deque/48101-2_neg.cc: New test.
	* testsuite/23_containers/deque/48101_neg.cc: New test.
	* testsuite/23_containers/forward_list/48101-2_neg.cc: New test.
	* testsuite/23_containers/forward_list/48101_neg.cc: New test.
	* testsuite/23_containers/list/48101-2_neg.cc: New test.
	* testsuite/23_containers/list/48101_neg.cc: New test.
	* testsuite/23_containers/map/48101-2_neg.cc: New test.
	* testsuite/23_containers/map/48101_neg.cc: New test.
	* testsuite/23_containers/multimap/48101-2_neg.cc: New test.
	* testsuite/23_containers/multimap/48101_neg.cc: New test.
	* testsuite/23_containers/multiset/48101-2_neg.cc: New test.
	* testsuite/23_containers/multiset/48101_neg.cc: New test.
	* testsuite/23_containers/set/48101-2_neg.cc: New test.
	* testsuite/23_containers/set/48101_neg.cc: New test.
	* testsuite/23_containers/unordered_map/48101-2_neg.cc: New test.
	* testsuite/23_containers/unordered_map/48101_neg.cc: New test.
	* testsuite/23_containers/unordered_multimap/48101-2_neg.cc: New test.
	* testsuite/23_containers/unordered_multimap/48101_neg.cc: New test.
	* testsuite/23_containers/unordered_multiset/48101-2_neg.cc: New test.
	* testsuite/23_containers/unordered_multiset/48101_neg.cc: New test.
	* testsuite/23_containers/unordered_set/48101-2_neg.cc: New test.
	* testsuite/23_containers/unordered_set/48101_neg.cc: New test.
	* testsuite/23_containers/vector/48101-2_neg.cc: New test.
	* testsuite/23_containers/vector/48101_neg.cc: New test.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 52219 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20171121/eca0e0b1/attachment.bin>


More information about the Libstdc++ mailing list