I want to create a functor like std::plus<>()
but also add std::clamp
functionality to it.
Let's say it is a function plus_clamp<>(T MIN = numeric_limits<T>::min(), T MAX = ...max())
I started to look what implementation does std::plus<>()
have, and tried to repeat it.
So I wrote something like this.
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;
}
};
But when I try to use that function with std::transform(a.begin(),a.end(),b.begin(),result.begin(), plus_clamp<>())
I get compiler error
error: expected '(' for function-style cast or type construction
error: expected expression(at template parameter arg)
error: expected expression(at function arguments)
I know that I am doing that wrong as I need to pass the template argument and the function arguments explicitly, but then this raises the question how is std::plus<>()
implemented so it skips the template and function arguments?