https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129000
Bug ID: 129000
Summary: [C++17] Unordered container deduction guides
unnecessarily instantiate <int> specializations for
size_type
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: bin.macchiato at gmail dot com
Target Milestone: ---
In C++17 and later, the deduction guides for unordered associative containers
hardcode <int> / <int, int> specializations to name the bucket-count size_type
parameter (both C++17 guides and C++23 from_range_t guides, in both normal and
debug headers):
1. Normal headers:
- libstdc++-v3/include/bits/unordered_set.h:
unordered_set guides (lines 1014-1123): unordered_set<int>::size_type
unordered_multiset guides (lines 2032-2148):
unordered_multiset<int>::size_type
- libstdc++-v3/include/bits/unordered_map.h:
unordered_map guides (lines 1341-1451): [typename] unordered_map<int,
int>::size_type
unordered_multimap guides (lines 2402-2515): unordered_multimap<int,
int>::size_type
2. Debug headers:
- libstdc++-v3/include/debug/unordered_set (lines 741-1635):
unordered_set<int>::size_type / unordered_multiset<int>::size_type
- libstdc++-v3/include/debug/unordered_map (lines 876-1800):
[typename] unordered_map<int, int>::size_type / unordered_multimap<int,
int>::size_type
For example, in libstdc++-v3/include/bits/unordered_set.h:
template<typename _InputIterator,
typename _Hash = hash<typename
iterator_traits<_InputIterator>::value_type>,
typename _Pred = equal_to<typename
iterator_traits<_InputIterator>::value_type>,
typename _Allocator = allocator<typename
iterator_traits<_InputIterator>::value_type>,
typename = _RequireInputIter<_InputIterator>,
typename = _RequireNotAllocatorOrIntegral<_Hash>,
typename = _RequireNotAllocator<_Pred>,
typename = _RequireAllocator<_Allocator>>
unordered_set(_InputIterator, _InputIterator,
unordered_set<int>::size_type = {},
_Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
-> unordered_set<typename iterator_traits<_InputIterator>::value_type,
_Hash, _Pred, _Allocator>;
Because unordered_set<int> (and similarly unordered_multiset<int>,
unordered_map<int, int>, unordered_multimap<int, int>) is a non-dependent class
template specialization, looking up ::size_type during phase-1 parsing forces
the compiler to implicitly instantiate these four container specializations and
their underlying _Hashtable specializations whenever <unordered_set> or
<unordered_map> is included in C++17+ mode, even if the translation unit never
uses those types or CTAD.
This eager instantiation can be observed with the following diagnostic probe
(not valid user code under [namespace.std], only used here to show that
implicit instantiation already occurred inside the header):
#include <unordered_set>
namespace std { template<> struct unordered_set<int> {}; }
With -std=c++14, g++ accepts the probe (no implicit instantiation on #include).
With -std=c++17, g++ rejects it with:
error: specialization of 'std::unordered_set<int>' after instantiation
Since __detail::_Hashtable_base::size_type in
libstdc++-v3/include/bits/hashtable_policy.h (line 1402) is unconditionally
defined as size_t for all allocators, using size_t directly (or a lightweight
helper alias matching _Hashtable_base::size_type, which also avoids
instantiating the deduced container during overload resolution and stays
aligned with LWG 2982 if size_type ever becomes allocator-dependent) would
avoid these unnecessary <int> / <int, int> instantiations.
I noticed this while checking libstdc++ after recently working on LWG 2982 in
libc++ (https://github.com/llvm/llvm-project/pull/239972). Would a cleanup
patch along these lines be welcome? If so, I would be happy to prepare and
submit one—do you prefer using size_t directly or a helper alias, and is there
anything libstdc++-specific I should keep in mind?