I am just playing with std::function<>
and operator
s, to make C++ statements look like Functional Languages(F#
) and found out that there is a difference between operator()
and operator<<
. My code :
Function 1 (Operator Overload):
function<int(int)> operator>>(function<int(int)> f1, function<int(int)> f2)
{
function<int(int)> f3 = [=](int x){return f1(f2(x));};
return f3;
}
Function 2 (Operator Overload):
function<int(int, int)> operator>>(function<int(int, int)> f1, function<int(int)> f2)
{
function<int(int, int)> f3 = [=](int x,int y){return f2(f1(x, y));};
return f3;
}
Function 3 (Operator Overload):
function<int(int)> operator()(function<int(int, int)> f1, int x)
{
function<int(int)> f2 = [=](int y){return f1(x, y);};
return f2;
}
while the Function 1 and Function 2 ( or Operator Overload ), Function 3 gives out error that :
error: ‘std::function<int(int)> operator()(std::function<int(int, int)>, int)’ must be a nonstatic member function
function<int(int)> operator()(function<int(int, int)> f1, int x)
^
Why do operator()
needs to be non-static member?
I think its different than What is the difference between the dot (.) operator and -> in C++? In that question the answer is explained in terms of pointers. But here I am using simple operator()
and operator>>
, which has nothing to do with pointers.