Nested Function Pointer Syntax

f0(..., (f1)( ..., (f2)(...), ...), ... )

I'm totally lost on some function pointer syntax. I'm working on a hw assignment and I've built some automated execution time analysis stuff to look at templatized sort functions that can accept vectors of general elements where these elements have a correpsponding comparison function defined to determine element ordering. This comparison function is passed as a callback function to the templatized Sort function because non-primitive types of data can't rely on the built in relational operators to establish ordering (ex Ball_1 < Ball_2). Basically I'm trying to sort general elements given a defined ordering for those elements, which could be strings, stacks, queues, maps, etc, so long as a definite ordering can be defined for the Sort function to work with them.

I'm not sure how to define the function header for the timing analysis function I'm writing, or even how to call this function once properly defined. The difficulty I'm stuck with is that I need to pass a Sort function parameter to the timing function (ie which sorting method do you wish to time analyze?), but this Sort function also has a function argument of its own which is the specialized comparison function used to determine order (ie for these elements you wish to sort how does one determine relational order? <, >, == ). This function argument of a function with its own function argument has got me baffled.

For the sake of being concrete the particular sorting problem I'm dealing with is that I need to sort a vector of integer sets, where set order is defined by the sum of their elements. I have the following function definition shown below, which I'm calling in main() with the following line, where VectorOfSetsOfIntegers is a vector containing a list of integer sets, along with the needed CompareSets comparison function needed to define the set ordering just mentioned. And Sort is a templatized sorting method that operates on vectors of any type, given their comparison function. Any help would be greatly appreciated, you'd really be teaching me some deep syntax.

Calling the Function (is this how it's done?):
GeneralTimeComputation( VectorOfSetsOfIntegers, Sort);


Function Definition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <typename Type>
double GeneralTimeComputation(Vector<Type> &elems2sort, void (sort_func)(Vector<Type> &, int (cmpFn)(Type, Type)) ){
  double start, finish, elapsed = 0;
  int iterations = 1;
  Vector<Type> elems2sortCOPY = elems2sort;

  start = double(clock()) / CLOCKS_PER_SEC;
  while(true){
    (sort_func)(elems2sortCOPY, cmpFn);
    finish = double(clock()) / CLOCKS_PER_SEC;

    elapsed = finish - start;
    if(elapsed == 0){
      iterations++;
      elems2sortCOPY = elems2sort; // to repeat the sorting operation with unsorted elements
    }
    else
        break;
  }
  return (elapsed / (double) iterations);
}

Last edited on
Er, I don't quite get your question... but if it is "how to pass a function(-object) to another function": do it like the stl does!
1
2
3
4
template <typename T> void do_it(T t, int x)
{
        t(x);
}


Now the requirement for T is that it can be called with an int-parameter. Thus, it will suffice:
1
2
3
4
void f(int x)
{
        std::cout<<x<<std::endl;
}

Or, if you need extra control over the function, use a function object:
1
2
3
4
5
6
7
8
9
struct A
{
        int z;
        A(int y) : z(y) {}
        void operator()(int x)
        {
                std::cout<<x+z<<std::endl;
        }
};

now you can use "do_it" with both:
1
2
3
4
5
int main()
{
        do_it(A(2), 1);
        do_it(f, 1);
}


The output will be:
3
1
Last edited on
I restated my question above, I'mma study your response right now too, thanks for the interest.
Aha, seems there's a built in compiler restriction to such a call, someone refered me to the following MSDN article

http://msdn.microsoft.com/en-us/library/wykfwk36(VS.71).aspx

Visual C++ Concepts: Building a C/C++ Program
Compiler Error C2896
'function1' : cannot use function template 'function2' as argument

A function template cannot be an argument to another function template.

Example

1
2
3
4
5
6
7
8
// C2896.cpp
template<class T1, class T2> void f1(void(*)(T1, T2));
template<class T1, class T2> void f2(T1, T2);

void g()
{
   f1(f2);   // C2896
}
Last edited on
Topic archived. No new replies allowed.