任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。
运算符函数对象
C++ 针对常用的算术和逻辑运算定义了很多函数对象:
比较
实现 x <= y 的函数对象
std::less_equal
template< class T > | (C++14 前) | |
template< class T = void > | (C++14 起) |
进行比较的函数对象。调用类型 T
上的 operator<= ,除非特化。
特化
std::less_equal
的特化为任何指针类型产生严格全序,即使内建的 operator<= 不如此。严格全序在 std::less 、 std::greater 、 std::less_equal 和 std::greater_equal 对该指针类型的特化间一致,且亦与对应的内建运算符( <
、 >
、 <=
及 >=
)所强加的部分顺序一致。
若特化 | (C++14 起) |
标准库提供
| (C++14 起 |
成员类型
| (C++20 前 |
成员函数
operator() | 检查第一参数是否小于或等于第二个 (公开成员函数) |
std::less_equal::operator()
bool operator()( const T& lhs, const T& rhs ) const; | (C++14 前) | |
constexpr bool operator()( const T& lhs, const T& rhs ) const; | (C++14 起) |
检查 lhs
是否小于或等于 rhs
。
参数
lhs, rhs | - | 要比较的值 |
返回值
若 lhs <= rhs 则为 true ,否则为 false 。
异常
(无)
可能的实现
constexpr bool operator()(const T &lhs, const T &rhs) const
{return lhs <= rhs;
}
调用示例
#include <iostream>
#include <functional>struct Cell
{int x;int y;Cell() = default;Cell(int a, int b): x(a), y(b) {}Cell(const Cell &cell){x = cell.x;y = cell.y;}Cell &operator+(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator+=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator*=(int n){x *= n;y *= n;return *this;}Cell &operator++(){x += 1;y += 1;return *this;}friend Cell operator +(const Cell &cell1, const Cell &cell2){Cell cell = cell1;cell += cell2;return cell;}friend Cell operator *(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x * cell2.x, cell1.y * cell2.y};return cell;}friend Cell operator /(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x / cell2.x, cell1.y / cell2.y};return cell;}friend Cell operator %(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x % cell2.x, cell1.y % cell2.y};return cell;}friend bool operator ==(const Cell &cell1, const Cell &cell2){return cell1.x == cell2.x && cell1.y == cell2.y;}friend bool operator !=(const Cell &cell1, const Cell &cell2){return cell1.x != cell2.x && cell1.y != cell2.y;}friend bool operator <(const Cell &cell1, const Cell &cell2){if (cell1.x == cell2.x){return cell1.y < cell2.y;}else{return cell1.x < cell2.x;}}friend bool operator >(const Cell &cell1, const Cell &cell2){if (cell1.x == cell2.x){return cell1.y > cell2.y;}else{return cell1.x > cell2.x;}}friend bool operator <=(const Cell &cell1, const Cell &cell2){if (cell1.x > cell2.x){return cell1.y <= cell2.y;}else{return cell1.x <= cell2.x;}}friend bool operator >=(const Cell &cell1, const Cell &cell2){if (cell1.x < cell2.x){return cell1.y >= cell2.y;}else{return cell1.x >= cell2.x;}}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}int main()
{std::cout << std::boolalpha;int *ptr = nullptr;std::cout << "std::less_equal<int*>()(1023, 1024): "<< std::less_equal<int*>()(ptr, nullptr) << std::endl;std::cout << "std::less_equal<char>()(50, 2): "<< std::less_equal<char>()(50, 2) << std::endl;std::cout << "std::less_equal<char>()('a', 97): "<< std::less_equal<char>()('a', 97) << std::endl;std::cout << "std::less_equal<int>()(1023, 1024): "<< std::less_equal<int>()(1023, 1024) << std::endl;std::cout << "std::less_equal<long>()(1023, 1024): "<< std::less_equal<long>()(1023, 1024) << std::endl;std::cout << "std::less_equal<long long>()(1023, 1024): "<< std::less_equal<long long>()(1023, 1024) << std::endl;std::cout << "std::less_equal<uint8_t>()(1023, 1024): "<< std::less_equal<uint8_t>()(8, 32) << std::endl;std::cout << "std::less_equal<uint16_t>()(123, 456): "<< std::less_equal<uint16_t>()(123, 456) << std::endl;std::cout << "std::less_equal<uint32_t>()(101, 202): "<< std::less_equal<uint32_t>()(101, 202) << std::endl;std::cout << "std::less_equal<uint64_t>()(10230, 10240): "<< std::less_equal<uint64_t>()(10230, 10240) << std::endl;std::cout << "std::less_equal<int8_t>()(1023, 1024): "<< std::less_equal<int8_t>()(8, 32) << std::endl;std::cout << "std::less_equal<int16_t>()(123, 456): "<< std::less_equal<int16_t>()(123, 456) << std::endl;std::cout << "std::less_equal<int32_t>()(101, 202): "<< std::less_equal<int32_t>()(101, 202) << std::endl;std::cout << "std::less_equal<int64_t>()(10230, 10240): "<< std::less_equal<int64_t>()(10230, 10240) << std::endl;std::cout << "std::less_equal<double>()(3.14, 3.14): "<< std::less_equal<double>()(3.14, 3.14) << std::endl;std::cout << "std::less_equal<float>()(3.14, 3.14): "<< std::less_equal<float>()(3.14, 3.14) << std::endl;std::cout << "std::less_equal<float>()(3, 3): "<< std::less_equal<float>()(3, 3) << std::endl;std::cout << "std::less_equal<float>()(3.56, 3.14): "<< std::less_equal<float>()(3.56, 3.14) << std::endl;std::cout << "std::less_equal<int>()(3.14, 3.14): "<< std::less_equal<int>()(3.34, 3.34) << std::endl;std::cout << "std::less_equal<Cell>()(Cell{101, 101}, Cell{202, 202}): "<< std::less_equal<Cell>()(Cell{101, 101}, Cell{202, 202}) << std::endl;std::cout << "std::less_equal<std::string>()(\"I am a \", \"handsome programmer\"):"<< std::less_equal<std::string>()("I am a ", "handsome programmer") << std::endl;return 0;
}
输出
std::less_equal<int*>()(1023, 1024): true
std::less_equal<char>()(50, 2): false
std::less_equal<char>()('a', 97): true
std::less_equal<int>()(1023, 1024): true
std::less_equal<long>()(1023, 1024): true
std::less_equal<long long>()(1023, 1024): true
std::less_equal<uint8_t>()(1023, 1024): true
std::less_equal<uint16_t>()(123, 456): true
std::less_equal<uint32_t>()(101, 202): true
std::less_equal<uint64_t>()(10230, 10240): true
std::less_equal<int8_t>()(1023, 1024): true
std::less_equal<int16_t>()(123, 456): true
std::less_equal<int32_t>()(101, 202): true
std::less_equal<int64_t>()(10230, 10240): true
std::less_equal<double>()(3.14, 3.14): true
std::less_equal<float>()(3.14, 3.14): true
std::less_equal<float>()(3, 3): true
std::less_equal<float>()(3.56, 3.14): false
std::less_equal<int>()(3.14, 3.14): true
std::less_equal<Cell>()(Cell{101, 101}, Cell{202, 202}): true
std::less_equal<std::string>()("I am a ", "handsome programmer"):true