1

I was learning function pointers and came across a piece of code here. The + is suppose to work as an operator/function

void FunctionA (float a, float b, float (*pt2Func)(float, float))
{
   float result = pt2Func(a, b);    // call using function pointer
}



void FunctionB ()
{
   Switch(2, 5, /* '+' specifies function 'Plus' to be executed */ '+');
}

So i experimented with it a little and this doesn't even compile.

Q. Why cant i use '<' as an operator here ?

Q. If not then does the user of the function need to write a separate code of compare two things whose comparision exists already...?

template <typename T>
bool compare (T a,T b,bool (*compareFunction) (T ,T ))
{
    return compareFunction (a,b);
}

int main ()
{
    int a=5,b=6;

    if (compare<int>(a,b,'<'))
        cout<<"Sucess";
    else
        cout<<"Post this in stack overflow ";

  return 0;
}

Update :

Some of the comments advice me to use this

(template <typename T, typename F> bool compare(T a, T b, F func))

But if i use this then this would only work for standard operators like std::less<int> . I want my function to work for both standard and non-standard operators (defined through their fucntions) both.

So the question still remains unanswered.

Community
  • 1
  • 1
Rohith R
  • 1,309
  • 2
  • 17
  • 36

0 Answers0