2

I'm told to create template of function , that will take 4 arguments :

  • pointer
  • reference
  • pointer to array
  • pointer to function

How to perform this task ? I was trying :

#include <iostream>
using namespace std;

int nothing(int a)
{
    return a;
}

template<typename T> T func(int *L, int &M, char *K, int (*P)(int))
{
    cout << L << "," << M << "," << K[0] << "," << P() << endl;
    return 0;    
}

int main()
{
    int x = 3;
    int *z = &x;
    int &y = x;
    char c[3];
    int (*pf)(int) = nothing;

    cout << "some result of func" << func(z, y, c, pf) << endl;

    system("pause");
    return 0;
}

This gives me "no matching function , I guess for 'pf'. Also now I have no control over what to pass within pf or am I wrong ?

1
  • 2
    As sbi noted, don't just edit your question in a way that makes answers already given look wrong. Apr 27, 2010 at 11:25

3 Answers 3

3

You're almost there. However, in C++, a reference is denoted with & (not $), a pointer to an array is a pointer to its first element, and a function pointer needs additional parentheses: T (*pf)().

Note that it is called a function template (as opposed to class templates).

Edit: (You shouldn't edit your question so that answers given so far suddenly become nonsensical.)

pf(x) calls the function stored in pf. pf already is a function pointer, so pass it as it is.
(Also, in your declaration P is a function taking an X, while pf takes an int. I suppose this is an editing error?)

Note that, with function pointers, there are 1..N types involved, one result type, and 0..N argument types. "Create a function template that will take a pointer to a function" can mean any of that. Or it means

template< typename F >
void f(F func);

which can be called with any function pointer.

2
  • and the size, you should pass the size as an additional argument Apr 27, 2010 at 10:24
  • BTW, I don't think "function template" is meant as "templated function" but rather as a template on how to create a function. Depending on your level of study of C++, your professor might just be asking for a signature rather than a templated function. Apr 27, 2010 at 10:26
1

To help you little bit more, try to remember how the "main" function taking arguments looks like, this will help you to see how you can make a pointer to an array.

2
  • I first liked this hint, but then I remembered that this is an array of pointers. So I'm afraid that's not really helpful for beginners.
    – sbi
    Apr 27, 2010 at 10:23
  • @sbi You are right, but it works and it is imho kind of language issue (array vs. pointer to first element). Apr 27, 2010 at 12:39
1

You now have some problems left...

TYPE (*P)(x) says you expect a pointer to function that takes an argument of type x - change it to an existing type.

In the expression func(z, y, c, pf(x)) you try to call the function pointer pf instead of just passing it.

Then you are calling func with parameters based on different types for the first 3 parameters, int and char, but func expects them to be based on the same type.
Try writing down with what types func will be called with and try matching that to a signature for func with TYPE being substituted to say int.

E.g. if you have the following:

template<typename T> void f(T* a, T* b);

and try to call it like this:

int* a = 0;
int* b = 0;
f(a, b);

the compiler instantiates and calls a function

void f<int>(int*, int*);

But if you do the following:

int*  a = 0;
char* b = 0;
f(a, b);

what should be called?

void f<int> (int*,  int* ); // doesn't match, 2nd argument is char*
void f<char>(char*, char*); // doesn't match, 1st argument is int*
2
  • Now I was trying : template<typename int> int func(int *L, int &M, char *K, int (*P)(int)) { cout << L << "," << M << "," << K[0] << "," << P() << endl; return 0; } but that gives me awful errors
    – anotr67
    Apr 27, 2010 at 10:47
  • @anotr67: No, i didn't mean to change the function but to imagine how it looks when the compiler substitutes TYPE with e.g. int. That might help understand why you get "no matching function". Apr 27, 2010 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.