C ++ Pointer to a function that takes an instance of a template class as an argument

I have a problem compiling a class that has pointers to functions as member variables. Pointers relate to functions that take an instance of a class as an argument.

how

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

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

Edit

Before using templates, I just had

double (*f)(Vector v);

It also works

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

But I would like to have a pointer to a function for a function that takes a common vector as an argument.

+3
source share
6 answers

Use item 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;
+2
source

" ", . 1.0 .

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

AcceptsVector f;

"" , "AcceptsVector" , . . f f Vector<T, N> .

+2

, , f ( T N?). , . .

0

, . ,

#include <vector>

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

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

( , int)

0

, :



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

- . ++, , - .

How do you do what you want? I am not sure, since I do not know what you are really trying to do and why.

0
source

Source: https://habr.com/ru/post/1743039/


All Articles