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?
There are a number of issues in your code.
std::transform), you need to pass them as constructor parameters toplus_clamp. This then means thatplus_clampneeds to be a template rather than the operator.nlisstd::numeric_limitsit is a template so needs the template type passed to it:std::numeric_limits<T>::min()std::plus<>()just constructing the type and not calling its operator. It should bestd::plus<>{}(lhs, rhs)std::transformyou passplus_clamp<>(), in your original codeplus_clampisn't a template so you just needplus_clamp().A fully working example would be:
If you are using c++17 you can use class template argument deduction and just use
plus_clamp(10, 100).