- memory_resource[meta header]
- std::pmr[meta namespace]
- function[meta id-type]
- cpp17[meta cpp]
namespace std::pmr {
template <class Tp = byte>
class polymorphic_allocator {
public:
friend bool operator==(const polymorphic_allocator& a,
const polymorphic_allocator& b) noexcept; // (1)
};
template <class T1, class T2>
bool operator==(const polymorphic_allocator<T1>& a,
const polymorphic_allocator<T2>& b) noexcept; // (2)
}
2つのpolymorphic_allocator
オブジェクトを等値比較する。
*a.resource() == *b.resource()
C++20以降、これらの演算子により以下の演算子が使用可能になる(制約は使用する==
に準ずる)。
friend bool operator!=(const polymorphic_allocator& a,
const polymorphic_allocator& b) noexcept; // (3)
template <class T1, class T2>
bool operator!=(const polymorphic_allocator<T1>& a,
const polymorphic_allocator<T2>& b) noexcept;
(1)と(3)の演算子はHidden friendsとして定義される。
#include <iostream>
#include <memory_resource>
int main()
{
std::cout << std::boolalpha;
auto mr = std::pmr::monotonic_buffer_resource{};
std::pmr::polymorphic_allocator<int> alloc{ &mr };
// (1) : memory_resourceとの比較
std::cout << (alloc == &mr) << std::endl;
std::cout << (alloc == std::pmr::get_default_resource()) << std::endl;
std::cout << '\n';
std::pmr::polymorphic_allocator<int> alloc2{};
std::pmr::polymorphic_allocator<double> alloc3{ &mr };
// (2) : polymorphic_allocator同士の比較
std::cout << (alloc == alloc2) << std::endl;
std::cout << (alloc == alloc) << std::endl;
// 同じmemory_resourceを利用していればtrue
std::cout << (alloc == alloc3) << std::endl;
}
- ==[color ff0000]
- monotonic_buffer_resource[link /reference/memory_resource/monotonic_buffer_resource.md]
true
false
false
true
true
- C++17
- Clang: ??
- GCC: 9.1 [mark verified]
- Visual C++: 2017 update 6 [mark verified]