任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。
运算符函数对象
C++ 针对常用的算术和逻辑运算定义了很多函数对象:
算术运算
实现 x * y 的函数对象
std::multiplies
template< class T > | (C++14 前) | |
template< class T = void > | (C++14 起) |
进行乘法的函数对象。等效地在二个 T
类型实例上调用 operator* 。
特化
标准库提供
| (C++14 起) |
成员类型
| (C++20 前) |
成员函数
operator() | 返回二个参数的积 (公开成员函数) |
std::multiplies::operator()
T operator()( const T& lhs, const T& rhs ) const; | (C++14 前) | |
constexpr T operator()( const T& lhs, const T& rhs ) const; | (C++14 起) |
返回 lhs
与 rhs
的积。
参数
lhs, rhs | - | 要相乘的值 |
返回值
lhs * rhs 的结果。
异常
(无)
可能的实现
constexpr T 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;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
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;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
int main()
{
std::cout << "std::multiplies<char>()(50, 2): "
<< std::multiplies<char>()(50, 2) << std::endl;
std::cout << "std::multiplies<int>()(1023, 1024): "
<< std::multiplies<int>()(1023, 1024) << std::endl;
std::cout << "std::multiplies<long>()(1023, 1024): "
<< std::multiplies<long>()(1023, 1024) << std::endl;
std::cout << "std::multiplies<long long>()(1023, 1024): "
<< std::multiplies<long long>()(1023, 1024) << std::endl;
std::cout << "std::multiplies<uint8_t>()(1023, 1024): "
<< std::multiplies<uint8_t>()(8, 32) << std::endl;
std::cout << "std::multiplies<uint16_t>()(123, 456): "
<< std::multiplies<uint16_t>()(123, 456) << std::endl;
std::cout << "std::multiplies<uint32_t>()(101, 202): "
<< std::multiplies<uint32_t>()(101, 202) << std::endl;
std::cout << "std::multiplies<uint64_t>()(10230, 10240): "
<< std::multiplies<uint64_t>()(10230, 10240) << std::endl;
std::cout << "std::multiplies<int8_t>()(1023, 1024): "
<< std::multiplies<int8_t>()(8, 32) << std::endl;
std::cout << "std::multiplies<int16_t>()(123, 456): "
<< std::multiplies<int16_t>()(123, 456) << std::endl;
std::cout << "std::multiplies<int32_t>()(101, 202): "
<< std::multiplies<int32_t>()(101, 202) << std::endl;
std::cout << "std::multiplies<int64_t>()(10230, 10240): "
<< std::multiplies<int64_t>()(10230, 10240) << std::endl;
std::cout << "std::multiplies<double>()(3.14, 3.14): "
<< std::multiplies<double>()(3.14, 3.14) << std::endl;
std::cout << "std::multiplies<float>()(3.14, 3.14): "
<< std::multiplies<float>()(3.14, 3.14) << std::endl;
std::cout << "std::multiplies<float>()(3, 3): "
<< std::multiplies<float>()(3, 3) << std::endl;
std::cout << "std::multiplies<int>()(3.14, 3.14): "
<< std::multiplies<int>()(3.34, 3.34) << std::endl;
std::cout << "std::multiplies<Cell>()(Cell{101, 101}, Cell{202, 202}): "
<< std::multiplies<Cell>()(Cell{101, 101}, Cell{202, 202}) << std::endl;
//编译失败
// std::cout << "std::multiplies<std::string>()(\"I am a \", \"handsome programmer\"):"
// << std::multiplies<std::string>()("I am a ", "handsome programmer") << std::endl;
return 0;
}
输出
std::multiplies<char>()(50, 2): d
std::multiplies<int>()(1023, 1024): 1047552
std::multiplies<long>()(1023, 1024): 1047552
std::multiplies<long long>()(1023, 1024): 1047552
std::multiplies<uint8_t>()(1023, 1024):
std::multiplies<uint16_t>()(123, 456): 56088
std::multiplies<uint32_t>()(101, 202): 20402
std::multiplies<uint64_t>()(10230, 10240): 104755200
std::multiplies<int8_t>()(1023, 1024):
std::multiplies<int16_t>()(123, 456): -9448
std::multiplies<int32_t>()(101, 202): 20402
std::multiplies<int64_t>()(10230, 10240): 104755200
std::multiplies<double>()(3.14, 3.14): 9.8596
std::multiplies<float>()(3.14, 3.14): 9.8596
std::multiplies<float>()(3, 3): 9
std::multiplies<int>()(3.14, 3.14): 9
std::multiplies<Cell>()(Cell{101, 101}, Cell{202, 202}): {20402,20402}