3

I have trouble compiling a class, which has function pointers as member variables. The pointers are to functions which take an instance of a class as argument.

Like

template<class T, int N>
double (*f)(Vector<T,N> v);

I get "error: data member 'f' cannot be a member template" Compiler is gcc 4.2.

Edit

Before using templates I just had

double (*f)(Vector v);

This also works

double (*f)(Vector<double,2> v)

But I would like to have a function pointer for a function which takes a generic Vector as argument..

2
  • 1
    Can you post the signature of a function that f can point to? Commented Apr 27, 2010 at 18:07
  • 2
    A bit more context would be helpful for suggesting what you should do. Commented Apr 27, 2010 at 18:11

6 Answers 6

2

Use a member typedef:

template <typename T, int N>
class Vector
{
public:

    /// type of function pointer
    typedef double (*FuncPtr)( const Vector& );

};

// concrete type
typedef Vector<double,10> VecDouble10;

// actual function
double func( const VecDouble10>& );

// usage
VecDouble10::FuncPtr fp = func;
Sign up to request clarification or add additional context in comments.

5 Comments

I don't understand the first typedef, what would be the alias name for the function pointer?
Could you explain your answer a bit more, why should it work with typedef if it does not without it?
There's no magic - it's just a "template" for a concrete typedef in a specialized template. You can explicitly use a type like double (*FP)( const Vector<double,10>& ), typedef just makes it cleaner.
Humm I still don't understand it.. Why are you just using const Vector & in the function pointer, shouldn't it be const vector<T,N>& if it's templated?
Because it's within the template definition - template parameters are assumed.
2

If you want to have a "template pointer", you could try a function object. The example below adds 1.0 to the wrapped function template.

struct AcceptsVector {
  template<typename T, int N>
  double operator()(Vector<T,N> v) const { return 1.0 + real_f(v); }
};

AcceptsVector f;

The difference to a "real" template pointer is that you cannot re-seat "AcceptsVector" to call another template, like you can do with normal function pointers. The binding is hardcoded at compile-time. However you can pass along f like a function pointer, and can call f with any Vector<T, N> like a template.

Comments

1

That isn't quite valid c++, basically what you're looking for are template-template parameters.

http://www.progdoc.de/papers/ttp/psi-ttp/psi-ttp.html explains all about them

Generally, if you get yourself into this situation, you want to find a workaroumd, because your code becomes illegible VERY quickly

Comments

0

Well, the compiler doesn't know at the compile time, how many f's are you going to have (one for each possible T and N?). Therefore the compiler cannot calculate how much memory do objects of your class need. That's why such constructs are prohibited.

Comments

0

You can't have a templated pointer to function, that makes no sense. But what you can do is

#include <vector>

template <typename T>
void foo(const std::vector<T>& v) {
   // do something
}

void (*ptr_foo)(const std::vector<int>&) = &foo<int>;

(here the function pointers a templated function, which template argument is explicitly set to int)

Comments

0

Because your code makes just as much sense as:



struct X
{
  template < typename T >
  std::vector<T> vect;
};

You're trying to make a member variable a template. This is just not a valid C++ construct and I seriously doubt it ever will be.

How do you do what you actually want? I'm not sure since I don't know what you actually are trying to accomplish and why.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.