Home > OS >  Problem using std::transform with lambdas VS std::transform with std::bind
Problem using std::transform with lambdas VS std::transform with std::bind

Time:08-03

I am using Clang 14.0.0 and C 20. My goal is to write a general function in order to use enter image description here

Here is the calling code:

int main() {
    
    std::vector<double> v = { 0.11, 0.53, 0.32, 0.29, 0.77, 0.45, 0.96, 0.0, 1.0 };
    std::vector<double> results;

    lerp_element(std::begin(v), std::end(v), std::back_inserter(results), 10.0, 20.0);
    for (auto x : results) { std::cout << x << ' '; }
 
    return 0;
}

Godbolt

Since I have the version with the lambda working, it seems the issue must be in the way I am using std::bind. I have been reading everything I could find online on how to properly use std::bind and std::placeholders, but obviously I must not be getting it.

I understand the problem has to do with std::lerp not receiving consistent argument types in order to resolve the overload, so:

  1. why does that happen?
  2. how do I modify the std::bind/std::bind_front versions to make it compile and work as expected?

CodePudding user response:

The function argument to std::bind or std::bind_front should not be invoked, but rather the function itself is passed as the first argument:

using T = std::iter_value_t<InputIterator>;
using F = T (*)(T, T, T) noexcept;

return std::transform(first, last, result,
                      std::bind_front<F>(std::lerp, a, b));

Godbolt

  • Related