Commit 1131840b authored by Raphael Kubo da Costa's avatar Raphael Kubo da Costa Committed by Commit Bot

flat_map: Readd constructors and assignment operator overloads.

https://chromium-review.googlesource.com/c/chromium/src/+/705955 ("base::flat_map
was missing defaults for duplication handling") removed them in favor of
inheriting them from flat_tree.

This can cause problems with GCC due to
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84782, and the build has been
failing with it since
https://chromium-review.googlesource.com/1000393 ("Mojo C++ Bindings:
unordered_map => flat_map") with a message like this:

    gen/mojo/public/interfaces/bindings/tests/test_unions.mojom.cc: In member function ‘void mojo::test::ObjectUnion::set_f_map_int8(const base::flat_map<std::__cxx11::basic_string<char>, signed char>&)’:
    gen/mojo/public/interfaces/bindings/tests/test_unions.mojom.cc:455:30: error: use of deleted function ‘base::flat_map<std::__cxx11::basic_string<char>, signed char>::flat_map(const base::flat_map<std::__cxx11::basic_string<char>, signed char>&)’
             std::move(f_map_int8));
                                  ^
    In file included from ../../mojo/public/cpp/bindings/clone_traits.h:11:0,
                     from gen/mojo/public/interfaces/bindings/tests/test_unions.mojom.h:22,
                     from gen/mojo/public/interfaces/bindings/tests/test_unions.mojom.cc:15:
    ../../base/containers/flat_map.h:152:7: note: ‘base::flat_map<std::__cxx11::basic_string<char>, signed char>::flat_map(const base::flat_map<std::__cxx11::basic_string<char>, signed char>&)’ is implicitly declared as deleted because ‘base::flat_map<std::__cxx11::basic_string<char>, signed char>’ declares a move constructor or move assignment operator
     class flat_map : public ::base::internal::flat_tree<
           ^~~~~~~~

Work around it by replacing the inheritance statements with actual
constructors and operator=() overloads again. This change does not
completely revert https://chromium-review.googlesource.com/1000393, as the
constructors maintain their new signature (so that they look like
flat_tree's).

Bug: 819294, 837221
Change-Id: I8f37a2c8aa269b3c1025cdc009518a7709e95947
Reviewed-on: https://chromium-review.googlesource.com/1038103
Commit-Queue: Raphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarkylechar <kylechar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#556168}
parent f128bbe0
...@@ -171,14 +171,37 @@ class flat_map : public ::base::internal::flat_tree< ...@@ -171,14 +171,37 @@ class flat_map : public ::base::internal::flat_tree<
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Lifetime and assignments. // Lifetime and assignments.
// //
// Note: we explicitly bring operator= in because otherwise // Note: we could do away with these constructors, destructor and assignment
// flat_map<...> x; // operator overloads by inheriting |tree|'s, but this breaks the GCC build
// x = {...}; // due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84782 (see
// Would first create a flat_map and then move assign it. This most likely // https://crbug.com/837221).
// would be optimized away but still affects our debug builds.
flat_map() = default;
explicit flat_map(const Compare& comp);
template <class InputIterator>
flat_map(InputIterator first,
InputIterator last,
FlatContainerDupes dupe_handling = KEEP_FIRST_OF_DUPES,
const Compare& comp = Compare());
flat_map(const flat_map&) = default;
flat_map(flat_map&&) noexcept = default;
flat_map(std::vector<value_type> items,
FlatContainerDupes dupe_handling = KEEP_FIRST_OF_DUPES,
const Compare& comp = Compare());
flat_map(std::initializer_list<value_type> ilist,
FlatContainerDupes dupe_handling = KEEP_FIRST_OF_DUPES,
const Compare& comp = Compare());
using tree::tree; ~flat_map() = default;
using tree::operator=;
flat_map& operator=(const flat_map&) = default;
flat_map& operator=(flat_map&&) = default;
// Takes the first if there are duplicates in the initializer list.
flat_map& operator=(std::initializer_list<value_type> ilist);
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Map-specific insert operations. // Map-specific insert operations.
...@@ -215,6 +238,49 @@ class flat_map : public ::base::internal::flat_tree< ...@@ -215,6 +238,49 @@ class flat_map : public ::base::internal::flat_tree<
friend void swap(flat_map& lhs, flat_map& rhs) noexcept { lhs.swap(rhs); } friend void swap(flat_map& lhs, flat_map& rhs) noexcept { lhs.swap(rhs); }
}; };
// ----------------------------------------------------------------------------
// Lifetime.
template <class Key, class Mapped, class Compare>
flat_map<Key, Mapped, Compare>::flat_map(const Compare& comp) : tree(comp) {}
template <class Key, class Mapped, class Compare>
template <class InputIterator>
flat_map<Key, Mapped, Compare>::flat_map(InputIterator first,
InputIterator last,
FlatContainerDupes dupe_handling,
const Compare& comp)
: tree(first, last, dupe_handling, comp) {}
template <class Key, class Mapped, class Compare>
flat_map<Key, Mapped, Compare>::flat_map(std::vector<value_type> items,
FlatContainerDupes dupe_handling,
const Compare& comp)
: tree(std::move(items), dupe_handling, comp) {}
template <class Key, class Mapped, class Compare>
flat_map<Key, Mapped, Compare>::flat_map(
std::initializer_list<value_type> ilist,
FlatContainerDupes dupe_handling,
const Compare& comp)
: flat_map(std::begin(ilist), std::end(ilist), dupe_handling, comp) {}
// ----------------------------------------------------------------------------
// Assignments.
template <class Key, class Mapped, class Compare>
auto flat_map<Key, Mapped, Compare>::operator=(
std::initializer_list<value_type> ilist) -> flat_map& {
// When https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84782 gets fixed, we
// need to remember to inherit tree::operator= to prevent
// flat_map<...> x;
// x = {...};
// from first creating a flat_map and then move assigning it. This most
// likely would be optimized away but still affects our debug builds.
tree::operator=(ilist);
return *this;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Insert operations. // Insert operations.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment