std::rel_op...rator!=,>,=(3)
π std::rel_op...rator!=,>,=(3) π
okay so bestie, welcome to the std::rel_op...rator!=,>,= page, straight out of the C++ Standard Libary manual β section 3, and yes the header really spells it βLibary,β weβre keeping the lore. this one is a little deprecated-era time capsule and itβs honestly iconic.
π NAME π
std::rel_ops::operator!=,>,= - std::rel_ops::operator!=,>,=
yes, thatβs the whole name, twice, because the manual said so and we respect the bit.
πΈ Synopsis πΈ
Defined in header <utility>:
template< class T > (1) (deprecated in C++20)
bool operator!=( const T& lhs, const T& rhs );
template< class T > (2) (deprecated in C++20)
bool operator>( const T& lhs, const T& rhs );
template< class T > (3) (deprecated in C++20)
bool operator<=( const T& lhs, const T& rhs );
template< class T > (4) (deprecated in C++20)
bool operator>=( const T& lhs, const T& rhs );
Given a user-defined operator== and operator< for objects of type T, these implement the usual semantics of the other comparison operators. itβs giving βderive the whole friend group from two core operatorsβ energy, fr.
- Implements
operator!=in terms ofoperator==. - Implements
operator>in terms ofoperator<. - Implements
operator<=in terms ofoperator<. - Implements
operator>=in terms ofoperator<.
π Parameters π
lhsβ left-hand argument.rhsβ right-hand argument.
β¨ Return value β¨
- Returns
trueiflhsis not equal torhs. - Returns
trueiflhsis greater thanrhs. - Returns
trueiflhsis less or equal torhs. - Returns
trueiflhsis greater or equal torhs.
π οΈ Possible implementation π οΈ
(1) operator!=
namespace rel_ops
{
template<class T>
bool operator!=(const T& lhs, const T& rhs)
{
return !(lhs == rhs);
}
}
(2) operator>
namespace rel_ops
{
template<class T>
bool operator>(const T& lhs, const T& rhs)
{
return rhs < lhs;
}
}
(3) operator<=
namespace rel_ops
{
template<class T>
bool operator<=(const T& lhs, const T& rhs)
{
return !(rhs < lhs);
}
}
(4) operator>=
namespace rel_ops
{
template<class T>
bool operator>=(const T& lhs, const T& rhs)
{
return !(lhs < rhs);
}
}
π¦ Notes π¦
Boost.operators provides a more versatile alternative to std::rel_ops.
heads up, bestie: as of C++20,std::rel_opsare deprecated in favor ofoperator<=>. so this is very βstill cute, but the new shiny thing existsβ energy.
π Example π
// Run this code
#include <iostream>
#include <utility>
struct Foo
{
int n;
};
bool operator==(const Foo& lhs, const Foo& rhs)
{
return lhs.n == rhs.n;
}
bool operator<(const Foo& lhs, const Foo& rhs)
{
return lhs.n < rhs.n;
}
int main()
{
Foo f1 = {1};
Foo f2 = {2};
using namespace std::rel_ops;
std::cout << std::boolalpha
<< "{1} != {2} : " << (f1 != f2) << '\n'
<< "{1} > {2} : " << (f1 > f2) << '\n'
<< "{1} <= {2} : " << (f1 <= f2) << '\n'
<< "{1} >= {2} : " << (f1 >= f2) << '\n';
}
π Output π
{1} != {2} : true
{1} > {2} : false
{1} <= {2} : true
{1} >= {2} : false
okay thatβs the whole page, bestie. source/version: http://cppreference.com; date: 2024.06.10; page: std::rel_op...rator!=,>,=(3). mwah π byeee π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms