我想创建一个仿函数,std::plus<>()
但也要为其添加std::clamp
功能。假设这是一个功能plus_clamp<>(T MIN = numeric_limits<T>::min(), T MAX = ...max())
,我开始查看实现std::plus<>()
有什么,并尝试重复它。
所以我写了这样的东西。
struct plus_clamp {
template<typename T = void>
constexpr T operator()(const T& lhs, const T& rhs, T MIN = nl::min(),T MAX = nl::max()) {
T result = std::plus<>();
result = std::clamp(result, MIN,MAX);
return result;
}
};
但是当我尝试使用该功能时std::transform(a.begin(),a.end(),b.begin(),result.begin(), plus_clamp<>())
我得到编译器错误
error: expected '(' for function-style cast or type construction
error: expected expression(at template parameter arg)
error: expected expression(at function arguments)
我知道我做错了,因为我需要显式传递模板参数和函数参数,但这引发了一个问题,它是如何std::plus<>()
实现的,所以它跳过了模板和函数参数?