Search code examples
c++c++11templateslambdastd-function

Can't use the lambda function inside function template


I have a problem with my function template.

I have 3 different collections and I've got iterators for the collections. Now I have to create an function template 'apply' which will do the following: 1. Pass through all elements of collections and check if predicate is true:

1.1 If predicate return true - then element of collection need to be changed with lambda 'passed'

1.2 if predicate return false = then element of collection need to be changed with lambda 'rejected'

Please give me an example how should I write it.

Thank you soo much for help. Updated code here:

#include <iostream>
#include <vector>
#include <list>
#include <functional>

using namespace std;

template<typename T>
void apply(T collBegin, T collEnd, function<bool(int)> f1, function<int(int)> f2, function<int(int)> f3)
{
    for (T actualPosition = collBegin; actualPosition != collEnd; ++actualPosition) {
        if (f1(*actualPosition)) {
            //the argument matches the predicate function f1
            *actualPosition = f2(*actualPosition);
        }
        else {
            //the argument doesn't match the predicate function f1
            *actualPosition = f3(*actualPosition);
        }
    }
}

int main()
{
    int arr[]{ 1,2,3,4,5,6,7,8,9 };

    auto predicate = [](int arg) -> bool { return arg % 2 == 0; };

    auto passed = [](int arg) -> int { return arg / 2; };

    auto rejected = [](int arg) -> int { return (3 * arg) + 1; };

    apply(arr, arr + std::size(arr), predicate, passed, rejected);

    std::vector<int> vec(arr, arr + std::size(arr));
    apply(vec.begin(), vec.end(), predicate, passed, rejected);

    std::list<int> lis(vec.begin(), vec.end());
    apply(lis.begin(), lis.end(), predicate, passed, rejected);


    for (auto e : lis) std::cout << e << " ";
    std::cout << '\n';
}

This code works. But I want to make change it from int to T. How can I do this ?


Solution

  • Sooo how the code should be looks like ? Can you write an example ?

    The following compiles and runs, but I am not sure if this is what you wanted:

    #include <iostream>
    #include <vector>
    #include <list>
    #include <functional>
    #include <algorithm>
    
    template<typename T, typename U>
    void apply(T collBegin, T collEnd, std::function<bool(U const &)> f1, std::function<U(U const &)> f2, std::function<U(U const &)> f3)
    {
        std::for_each(collBegin, collEnd, [&](auto &el) { el = f1(el) ? f2(el) : f3(el); });
    }
    
    int main()
    {
        std::function<bool(int const &)> predicate = [](int const &arg) -> bool { return arg % 2 == 0; };
        std::function<int(int const &)> passed = [](int const &arg) -> int { return arg / 2; };
        std::function<int(int const &)> rejected = [](int const &arg) -> int { return (3 * arg) + 1; };
    
        int arr[]{ 1,2,3,4,5,6,7,8,9 };
        apply(arr, arr + sizeof(arr)/sizeof(int), predicate, passed, rejected);
    
        std::vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
        apply(vec.begin(), vec.end(), predicate, passed, rejected);
    
        std::list<int> lis(vec.begin(), vec.end());
        apply(lis.begin(), lis.end(), predicate, passed, rejected);
    
        for (auto e : lis) std::cout << e << " ";
        std::cout << '\n';
    }
    

    https://ideone.com/A30Dl9

    1 2 16 4 4 5 34 1 7