- memory[meta header]
- std[meta namespace]
- function template[meta id-type]
- cpp20[meta cpp]
namespace std {
template <class T1, class D1, class T2, class D2>
requires three_way_comparable_with<
typename unique_ptr<T1, D1>::pointer,
typename unique_ptr<T2, D2>::pointer>
compare_three_way_result_t<
typename unique_ptr<T1, D1>::pointer,
typename unique_ptr<T2, D2>::pointer>
operator<=>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); // (1) C++20
template <class T, class D>
requires three_way_comparable_with<typename unique_ptr<T, D>::pointer>
compare_three_way_result_t<typename unique_ptr<T, D>::pointer>
operator<=>(const unique_ptr<T, D>& x, nullptr_t); // (2) C++20
template <class T, class D>
requires three_way_comparable_with<typename unique_ptr<T, D>::pointer>
constexpr compare_three_way_result_t<typename unique_ptr<T, D>::pointer>
operator<=>(const unique_ptr<T, D>& x, nullptr_t); // (2) C++23
}
- nullptr_t[link /reference/cstddef/nullptr_t.md]
unique_ptr
オブジェクトの三方比較を行う。
- (1) : 型
unique_ptr<T1, D1>::pointer
と型unique_ptr<T2, D2>::pointer
が三方比較可能であること - (2) : 型
unique_ptr<T, D>::pointer
同士が三方比較可能であること
-
(1) :
return compare_three_way()(x.get(), y.get());
- compare_three_way[link /reference/compare/compare_three_way.md]
- get()[link get.md]
-
(2) :
return compare_three_way()(x.get(), static_cast<typename unique_ptr<T, D>::pointer>(nullptr));
- compare_three_way[link /reference/compare/compare_three_way.md]
- get()[link get.md]
#include <iostream>
#include <memory>
int main()
{
std::unique_ptr<int> p1(new int(3));
if ((p1 <=> p1) == 0) {
std::cout << "equal" << std::endl;
}
std::unique_ptr<int> p2;
if ((p2 <=> nullptr) == 0) {
std::cout << "p2 is nullptr" << std::endl;
}
if ((nullptr <=> p2) == 0) {
std::cout << "p2 is nullptr" << std::endl;
}
}
equal
p2 is nullptr
p2 is nullptr
- C++20
- Clang:
- GCC: 10 [mark verified]
- Visual C++: ??