Repository: fmtlib/fmt
File: include/fmt/format.h (C/C++)

Code before:
    this->set(store_, SIZE);
    if (detail::is_constant_evaluated()) detail::fill_n(store_, SIZE, T());
  }
  FMT_CONSTEXPR20 ~basic_memory_buffer() { deallocate(); }

 private:
  template <typename Alloc = Allocator>
  typename std::enable_if<std::allocator_traits<Alloc>::
                              propagate_on_container_move_assignment::value,
                          bool>::type
  allocator_move_impl(basic_memory_buffer& other) {
    alloc_ = std::move(other.alloc_);
    return true;
  }
  // If the allocator does not propagate,
  // then copy the content from source buffer.
  template <typename Alloc = Allocator>
  typename std::enable_if<!std::allocator_traits<Alloc>::
                              propagate_on_container_move_assignment::value,
                          bool>::type
  allocator_move_impl(basic_memory_buffer& other) {
    T* data = other.data();
    if (alloc_ != other.alloc_ && data != other.store_) {
      size_t size = other.size(), capacity = other.capacity();
      // Perform copy operation, allocators are different
      this->reserve(size);
      detail::copy<T>(data, data + size, this->data());
      this->resize(size);
      other.clear();
      return false;
    }
    return true;
  }

  // Move data from other to this buffer.
  FMT_CONSTEXPR20 void move(basic_memory_buffer& other) {
    using alloc_traits = std::allocator_traits<Allocator>;
    T* data = other.data();
    size_t size = other.size(), capacity = other.capacity();
    // Replicate the behaviour of std library containers
    if (!allocator_move_impl(other)) {
      return;
    }
    if (data == other.store_) {
      this->set(store_, capacity);
      detail::copy<T>(other.store_, other.store_ + size, store_);
    } else {
      this->set(data, capacity);
      // Set pointer to the inline array so that delete is not called
      // when deallocating.
      other.set(other.store_, 0);

Review comment (suggestion):
AFAICT, `basic_memory_buffer::resize` calls `basic_memory_buffer::try_resize` which calls `buffer::try_resize`, which first calls `buffer::try_reserve` anyway.
Can we just call `this->resize(size)` then do the copy instead of making 2 calls?

(I was just browsing through this PR and thought I'd add a comment or two, it looks neat)

Code after:
  using const_reference = const T&;

  FMT_CONSTEXPR explicit basic_memory_buffer(
      const Allocator& alloc = Allocator())
      : detail::buffer<T>(grow), alloc_(alloc) {
    this->set(store_, SIZE);
    if (detail::is_constant_evaluated()) detail::fill_n(store_, SIZE, T());
  }
  FMT_CONSTEXPR20 ~basic_memory_buffer() { deallocate(); }

 private:
  template <typename Alloc = Allocator>
  FMT_CONSTEXPR20
      typename std::enable_if<std::allocator_traits<Alloc>::
                                  propagate_on_container_move_assignment::value,
                              bool>::type
      allocator_move_impl(basic_memory_buffer& other) {
    alloc_ = std::move(other.alloc_);
    return true;
  }
  // If the allocator does not propagate,
  // then copy the content from source buffer.
  template <typename Alloc = Allocator>
  FMT_CONSTEXPR20
      typename std::enable_if<!std::allocator_traits<Alloc>::
                                  propagate_on_container_move_assignment::value,
                              bool>::type
      allocator_move_impl(basic_memory_buffer& other) {
    T* data = other.data();
    if (alloc_ != other.alloc_ && data != other.store_) {
      size_t size = other.size();
      // Perform copy operation, allocators are different
      this->resize(size);
      detail::copy<T>(data, data + size, this->data());
      return false;
    }
    return true;
  }

  // Move data from other to this buffer.
  FMT_CONSTEXPR20 void move(basic_memory_buffer& other) {
    T* data = other.data();
    size_t size = other.size(), capacity = other.capacity();
    // Replicate the behaviour of std library containers
    if (!allocator_move_impl(other)) {
      return;
    }
    if (data == other.store_) {
      this->set(store_, capacity);
      detail::copy<T>(other.store_, other.store_ + size, store_);
    } else {