Skip to content

Commit

Permalink
Implement LWG-3683: operator== for polymorphic_allocator cannot d…
Browse files Browse the repository at this point in the history
…educe template argument in common cases (microsoft#2879)
  • Loading branch information
frederick-vs-ja authored and fsb4000 committed Aug 13, 2022
1 parent 89cc6a2 commit 52d5a3c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
12 changes: 12 additions & 0 deletions stl/inc/xpolymorphic_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ namespace pmr {
return _Resource;
}

_NODISCARD_FRIEND bool operator==(
const polymorphic_allocator& _Lhs, const polymorphic_allocator& _Rhs) noexcept {
return *_Lhs._Resource == *_Rhs._Resource;
}

#if !_HAS_CXX20
_NODISCARD_FRIEND bool operator!=(
const polymorphic_allocator& _Lhs, const polymorphic_allocator& _Rhs) noexcept {
return *_Lhs._Resource != *_Rhs._Resource;
}
#endif // !_HAS_CXX20

private:
memory_resource* _Resource = _STD pmr::get_default_resource();
};
Expand Down
41 changes: 41 additions & 0 deletions tests/std/tests/P0220R1_polymorphic_memory_resources/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cstdlib>
#include <deque>
#include <forward_list>
#include <functional>
#include <list>
#include <malloc.h>
#include <map>
Expand Down Expand Up @@ -946,6 +947,45 @@ namespace {
}
} // namespace eq

namespace eq_cvt {
void test() {
const auto pres = std::pmr::get_default_resource();
std::pmr::polymorphic_allocator<int> a = pres;
const auto ra = std::ref(a);
const auto cra = std::cref(a);

CHECK(a == pres);
CHECK(a == ra);
CHECK(a == cra);
CHECK(pres == a);
CHECK(pres == ra);
CHECK(pres == cra);
CHECK(ra == a);
CHECK(ra == pres);
CHECK(ra == ra);
CHECK(ra == cra);
CHECK(cra == a);
CHECK(cra == pres);
CHECK(cra == ra);
CHECK(cra == cra);

CHECK(!(a != pres));
CHECK(!(a != ra));
CHECK(!(a != cra));
CHECK(!(pres != a));
CHECK(!(pres != ra));
CHECK(!(pres != cra));
CHECK(!(ra != a));
CHECK(!(ra != pres));
CHECK(!(ra != ra));
CHECK(!(ra != cra));
CHECK(!(cra != a));
CHECK(!(cra != pres));
CHECK(!(cra != ra));
CHECK(!(cra != cra));
}
} // namespace eq_cvt

namespace destroy {
void test() {
bool destroyed = false;
Expand Down Expand Up @@ -1505,6 +1545,7 @@ int main() {
polymorphic_allocator::mem::select_on_container_copy_construction::test();
polymorphic_allocator::mem::resource::test();
polymorphic_allocator::eq::test();
polymorphic_allocator::eq_cvt::test();
polymorphic_allocator::destroy::test();

monotonic::ctor::buffer_upstream::test();
Expand Down

0 comments on commit 52d5a3c

Please sign in to comment.