Skip to content

Conversation

@frederick-vs-ja
Copy link
Contributor

@frederick-vs-ja frederick-vs-ja commented Dec 22, 2025

Per N5032 [container.reqmts]/66.1, exception safety of single element insertion is only guaranteed for standard containers. So perhaps we should guard the insertion for non-standard containers that do not have guaranteed (exception-safe) emplace.

The emplace calls are only guarded for containers whose emplace have no guaranteed exception safety. Currently, all supported standard containers have such exception safety, so this only affects non-standard containers.

Unblocked libcxx tests:

  • std/containers/container.adaptors/flat.set/flat.set.modifiers/emplace_hint.pass.cpp
  • std/containers/container.adaptors/flat.set/flat.set.modifiers/emplace.pass.cpp
  • std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_cv.pass.cpp
  • std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_iter_cv.pass.cpp
  • std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_iter_rv.pass.cpp
  • std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_rv.pass.cpp
  • std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_transparent.pass.cpp (for Clang only, due to MSVC not implementing WG21-P2448R2)

Fixes #5538.

@frederick-vs-ja frederick-vs-ja requested a review from a team as a code owner December 22, 2025 01:48
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews Dec 22, 2025
@frederick-vs-ja frederick-vs-ja changed the title <flat_set>: Make flat_(multi)set` insertion exception-safe for non-standard sequence containers <flat_set>: Make flat_(multi)set insertion exception-safe for non-standard sequence containers Dec 22, 2025
…ence containers

Per N5032 [container.reqmts]/66.1, exception safety of single element
insertion is only guaranteed for standard containers. So perhaps we
should guard the insertion for non-standard containers that do not have
guaranteed (exception-safe) `emplace`.

The `emplace` calls are only guarded for containers whose `emplace` have
no guaranteed exception safety. Currently, all supported standard
containers have such exception safety, so this only affects non-standard
containers.
@StephanTLavavej StephanTLavavej added bug Something isn't working flat_meow C++23 container adaptors labels Dec 31, 2025
@StephanTLavavej StephanTLavavej self-assigned this Dec 31, 2025
@vmichal
Copy link
Contributor

vmichal commented Jan 5, 2026

There are similar single-element insertion operations in <flat_map> that could benefit from similar approach IMO.

This seems like the exact same case

STL/stl/inc/flat_map

Lines 951 to 956 in 18e1eb5

void _Insert_exact(const_iterator _Position, key_type&& _Key_val, mapped_type&& _Mapped_val) {
_Clear_guard _Guard{this};
_Data.keys.insert(_Position._Key_it, _STD move(_Key_val));
_Data.values.insert(_Position._Mapped_it, _STD move(_Mapped_val));
_Guard._Target = nullptr;
}

This could maybe be a candidate, since we are repeatedly using a single-element insertion function?

STL/stl/inc/flat_map

Lines 1055 to 1075 in 18e1eb5

template <bool _NeedSorting, class _InputIterator, class _Sentinel>
void _Insert_range(_InputIterator _First, _Sentinel _Last) {
_Clear_guard _Guard{this};
const auto _Old_distance = static_cast<difference_type>(size());
// Insert the new elements at the end
for (; _First != _Last; ++_First) {
value_type _Val = *_First;
if constexpr (_Has_guaranteed_push_back<_KeyContainer>) {
_Data.keys.push_back(_STD move(_Val.first));
} else {
_Data.keys.insert(_Data.keys.end(), _STD move(_Val.first));
}
if constexpr (_Has_guaranteed_push_back<_MappedContainer>) {
_Data.values.push_back(_STD move(_Val.second));
} else {
_Data.values.insert(_Data.values.end(), _STD move(_Val.second));
}
}

In both cases, couldn't we relax our reaction a little bit and only clear the whole flat_(multi)map when a non-standard container is used?

@StephanTLavavej
Copy link
Member

@vmichal I don't think this is applicable to flat_map because of the separate keys and values. Even with Standard containers where emplacing a single key and a single value are both EH-strong, if the key succeeds but the value fails then the invariant is broken.

@StephanTLavavej StephanTLavavej removed their assignment Jan 5, 2026
@StephanTLavavej StephanTLavavej moved this from Initial Review to Merging in STL Code Reviews Jan 5, 2026
@StephanTLavavej StephanTLavavej merged commit eff33b6 into microsoft:feature/flat_map Jan 5, 2026
45 checks passed
@github-project-automation github-project-automation bot moved this from Merging to Done in STL Code Reviews Jan 5, 2026
@frederick-vs-ja frederick-vs-ja deleted the fix-flat_set-insertion branch January 6, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working flat_meow C++23 container adaptors

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants