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);
}
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;
cout << count_if(strings,strings+4,And<const char*>(NotNull(),BeginsWith('a'))) <<endl;
return 0;
}
Jpalecek solution is better and works as follows:
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);
}