Templates and function objects - C ++

I have a problem with this class.
the goal is for the main function to work correctly. we had to implement an AND function object for the code to work. I can not find what the problem is with our solution.
(the beginning and the end of the solution are noted in the comments in the code before the "main" function)
can you help?
thank

#include <iostream>
#include <algorithm>

using namespace std;

class NotNull
{
    public:
    bool operator()(const char* str) {return str != NULL;}
};

class BeginsWith
{
    char c;
    public:
    BeginsWith(char c) : c(c) {}
    bool operator()(const char* str) {return str[0] == c;}
};

class DividesBy {
    int mod;
    public:
    DividesBy(int mod) : mod(mod) {}
    bool operator()(int n) {return n%mod == 0;}
};

//***** This is where my sulotion starts ******

template <typename Function1, typename Function2, typename T>
class AndFunction
{
    Function1 f1;
    Function2 f2;
    public:
    AndFunction(Function1 g1, Function2 g2) : f1(g1), f2(g2) {}
    bool operator()(T t)
    {
        return (f1(t) && f2(t));
    }
};

template <typename Function1, typename Function2, typename T>
AndFunction <Function1, Function2, T>

bool And(Function1 f1, Function2 f2)
{
    return AndFunction<Function1, Function2, T>(f1, f2);
}

//***** This is where my sulotion ends ******

int main(int argc, char** argv)
{
    int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    char* strings[4] = {"aba", NULL, "air", "boom"};
    cout << count_if(array,array+10,And(DividesBy(2),DividesBy(4))) << endl;
    // prints 2, since 4 and 8 are the only numbers which can be divided by
    // both 2 and 4.
    cout << count_if(strings,strings+4,And(NotNull(),BeginsWith('a'))) <<endl;
    // prints 2, since only "aba" and "air" are both not NULL and begin
    // with the character 'a'.
    return 0;
}
+3
source share
4 answers

The template parameter Tcannot be inferred; it must be specified explicitly:

template <typename T, typename Function1, typename Function2>
AndFunction <Function1, Function2, T>
And(Function1 f1, Function2 f2)
{
    return AndFunction<Function1, Function2, T>(f1, f2);
}

//***** This is where my sulotion ends ******

int main(int argc, char** argv)
{
    int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    char* strings[4] = {"aba", NULL, "air", "boom"};
    cout << count_if(array,array+10,And<int>(DividesBy(2),DividesBy(4))) << endl;
    // prints 2, since 4 and 8 are the only numbers which can be divided by
    // both 2 and 4.
    cout << count_if(strings,strings+4,And<const char*>(NotNull(),BeginsWith('a'))) <<endl;
    // prints 2, since only "aba" and "air" are both not NULL and begin
    // with the character 'a'.
    return 0;
}

Jpalecek solution is better and works as follows:

//***** This is where my sulotion starts ******

template <typename Function1, typename Function2>
class AndFunction
{
    Function1 f1;
    Function2 f2;
    public:
    AndFunction(Function1 g1, Function2 g2) : f1(g1), f2(g2) {}
  template<typename T> bool operator()(T)
    {
        return (f1(t) && f2(t));
    }
};

template <typename Function1, typename Function2>
AndFunction <Function1, Function2>
And(Function1 f1, Function2 f2)
{
    return AndFunction<Function1, Function2>(f1, f2);
}

//***** This is where my sulotion ends ******
0
source

, T . , T (.. operator() -)?

+3

() : return AndFunction<Function1, Function2, T>(f1, f2); ( () ;), , , bool.


EDIT: , (bool And(Function1 f1, Function2 f2) ) bool, count_if ()

+2

, unary_function binary_function , , STL. :

template<typename Func1, typename Func2,typename T>
struct AndFunction : public unary_function<T,bool>{
    AndFunction(Func1 _func1, Func2 _func2) 
        : _myFunc1(_func1),
        _myFunc2(_func2){}

    bool operator()(T _t){
        return _myFunc1(_t) && _myFunc2(_2);
    }

private:
    Func1 _myFunc1;
    Func2 _myFunc2;
};

template<typename Func1, typename Func2, typename T> 
AndFunction<Func1, Func2, T> And(Func1 _func1, Func2 _func2){
    return AndFunction<Func1,Func2,T>(_func1,_func2);
};

so that you do not confuse the operator with the creation of the object and indicate how you should receive instructions on the functions.

On the flip side, how does yours work main, I think you really just want

struct And : public binary_function<bool, bool, bool>{
    bool operator()(bool _1, bool _2){
        return _1 && _2;
    }
};

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1755960/


All Articles