diff --git a/include/continuable/detail/traverse.hpp b/include/continuable/detail/traverse.hpp index 59bfe9a..ecd0923 100644 --- a/include/continuable/detail/traverse.hpp +++ b/include/continuable/detail/traverse.hpp @@ -441,6 +441,25 @@ template using container_mapping_tag_of_t = container_mapping_tag::value, can_reuse::value>; +/// Deduces to a true type if the given parameter T supports a `reserve` method +template +struct is_reservable_from : std::false_type {}; +template +struct is_reservable_from().reserve( + std::declval().size()))>> : std::true_type { +}; + +template +void reserve_if(std::true_type, Dest&& dest, Source&& source) { + // Reserve the mapped size + dest.reserve(source.size()); +} +template +void reserve_if(std::false_type, Dest&&, Source&&) noexcept { + // We do nothing here, since the container doesn't support reserving +} + /// We create a new container, which may hold the resulting type template auto remap_container(container_mapping_tag, M&& mapper, @@ -456,9 +475,10 @@ auto remap_container(container_mapping_tag, M&& mapper, auto remapped = rebind_container>(container); // We try to reserve the original size from the source - // container to the destination container. - // TODO Re-implement this - // traits::detail::reserve_if_reservable(remapped, container.size()); + // container inside the destination container. + reserve_if( + is_reservable_from, std::decay_t>{}, + remapped, container); // Perform the actual value remapping from the source to // the destination.