- optional[meta header]
- std[meta namespace]
- function template[meta id-type]
- cpp17[meta cpp]
namespace std {
template <class T, class U>
constexpr bool operator>=(const optional<T>& x, const optional<U>& y); // (1)
template <class T>
constexpr bool operator>=(const optional<T>& x, nullopt_t) noexcept; // (2)
template <class T>
constexpr bool operator>=(nullopt_t, const optional<T>& y) noexcept; // (3)
template <class T, class U>
constexpr bool operator>=(const optional<T>& x, const U& y); // (4)
template <class T, class U>
constexpr bool operator>=(const U& x, const optional<T>& y); // (5)
}- nullopt_t[link /reference/optional/nullopt_t.md]
optionalにおいて、左辺が右辺以上かの判定を行う。
- (1), (4), (5) : 型
Tと型Uが>=演算子で比較可能であること
- (1) :
xとyがどちらも有効値を持っていれば、有効値同士を>=演算子で比較した結果を返す。yが有効値を持っていなければtrueを返す。xが有効値を持っていなければfalseを返す - (2) :
trueを返す - (3) :
!x.has_value()を返す - (4) :
return x.has_value()? x.value()>= y : false; - (5) :
return y.has_value()? x >= y.value(): true;
#include <cassert>
#include <optional>
int main()
{
// optionalオブジェクト同士の比較
{
std::optional<int> a = 3;
std::optional<int> b = 1;
std::optional<int> none;
assert(a >= b);
assert(a >= none);
assert(!(none >= a));
}
// optionalオブジェクトとnulloptの比較
{
std::optional<int> p = 3;
std::optional<int> none;
assert(p >= std::nullopt);
assert(none >= std::nullopt);
assert(!(std::nullopt >= p));
assert(std::nullopt >= none);
}
// optionalオブジェクトと有効値の比較
{
std::optional<int> p = 3;
std::optional<int> none;
assert(p >= 1);
assert(5 >= p);
assert(!(none >= 3));
assert(3 >= none);
}
}- std::nullopt[link /reference/optional/nullopt_t.md]
- C++17
- Clang, C++17 mode: 4.0.1
- GCC, C++17 mode: 7.2
- ICC: ??
- Visual C++: ??