0

I was learning function pointer. I faced one syntax error. I have implemented sort with comparison function as an additonal arguement.

// working fine 
#include<iostream>
using namespace std;
typedef int (*C_MP)(double d);
template<typename X>
X* sort(X* start, X* last,X size, bool (*comp)(X,X)){
    for(int i=0;i<size;i++){
        for(int j=1;j<size;j++){
            X x=*(start+j-1);
            X y=*(start + j);
            if(!(*comp)(x,y)){
                cout<<i<<" "<<x<<" "<<y<<endl;
                swap(start[j],start[j-1]);
            }
        }
    }
    return start;
}

int fun(double x){
    return x;
}
bool comp(float x, float y){
    if(x>y){
        return 0;
    }
    return 1;
}
int main(){
   C_MP x = fun;
   cout<<(*x)(10)<<endl;
   float arr[] = {2.1,3.2,6.3,8.4,1.9,0.3};
   float* narr = sort<float>(&arr[0],&arr[5],6,&comp);
   for(int i=0;i<6;i++){
    cout<<*(narr+i)<<endl;
   }
}
}

In sort function definition, when I remove brackets from the comp it gives compilation error. I just wanted why this bracket is neccessary or is it just an syntax we need to remember and there is no logic behind it or compiler is confusing it with something else? please give some insights. Function pointers are also used in callbacks. Can you give some small idea/project ,if possible, where I can implement callbacks using function pointer.

// Give compilation error
#include<iostream>
using namespace std;
typedef int (*C_MP)(double d);
template<typename X>
X* sort(X* start, X* last,X size, bool *comp(X,X)){
    for(int i=0;i<size;i++){
        for(int j=1;j<size;j++){
            X x=*(start+j-1);
            X y=*(start + j);
            if(!(*comp)(x,y)){
                cout<<i<<" "<<x<<" "<<y<<endl;
                swap(start[j],start[j-1]);
            }
        }
    }
    return start;
}

int fun(double x){
    return x;
}
bool comp(float x, float y){
    if(x>y){
        return 0;
    }
    return 1;
}
int main(){
   C_MP x = fun;
   cout<<(*x)(10)<<endl;
   float arr[] = {2.1,3.2,6.3,8.4,1.9,0.3};
   float* narr = sort<float>(&arr[0],&arr[5],6,&comp);
   for(int i=0;i<6;i++){
    cout<<*(narr+i)<<endl;
   }
}

I tried using if similar question existed. I didn't found anything.

273K
  • 29,503
  • 10
  • 41
  • 64
  • 2
    use `std::function` the syntax is much simpler and the intent is clear – pm100 Jan 04 '23 at 04:31
  • Since it's already a function template there's not really any reason to use `std::function` over just making the comparator an additional template parameter. – Kyle Jan 04 '23 at 05:03
  • I concur for C++ do NOT spent time on learning about function pointers. C++ has both std::function and lambdas that help you with a lot of things that "C" style function pointers provide. And they are definitely easier to use when you have to call member functions of classes. – Pepijn Kramer Jan 04 '23 at 05:23
  • Since you are using templates and standard library stuff already, why not just use std::sort? At least study how you pass a (lambda) compare function to std::sort it will teach you a lot. – Pepijn Kramer Jan 04 '23 at 05:26
  • 1
    `bool (*func)(int)` is (as you found in your first case) the definition of a pointer to a function returning a `bool` and taking an `int` argument. Without the parentheses, `bool *func(int)` defines a function (not a pointer to one) that returns a pointer to a `bool`. – Adrian Mole Jan 04 '23 at 06:11
  • Thanks @AdrianMole, That clarifies my doubt. PepijnKramer I am aware about std::sort, I just implemented sort to appreciate the usecase of function pointer. I will look into std::function and lambdas. Thank you everyone – Harry pooper Jan 04 '23 at 08:20

0 Answers0