446,388 Members | 1,813 Online
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

# Problem with template member functions

 P: n/a I'm still working on the library for the simulation of SDEs. The following test code compiles fine with the digital mars compiler, but fails with gcc. Error: no matching function for call to 'prova(Diff_sine&, double, double)' #include #include "math.h" using namespace std; class Diff_sine { double theta; double gamma; double ea_Kc1, ea_Kc2; public: Diff_sine(double theta_, double gamma_); double ea_K(double x); }; inline Diff_sine::Diff_sine(double theta_, double gamma_) : theta(theta_), gamma(gamma_), ea_Kc1(theta_*gamma_/2), ea_Kc2(theta_*theta_/2) {} inline double Diff_sine::ea_K(double x) { return ea_Kc1*cos(gamma*x) + ea_Kc2*sin(gamma*x)*sin(gamma*x) ; } template double prova2(const F& theF, double a, double b) { return (theF.*f)(b); } template double prova(const F& theF, double a, double b) { double val; val = prova2(theF, a, b) ; return val; } int main() { Diff_sine theDiff_sine(1,1); double minimum ; minimum = prova(theDiff_sine, -1.0,1.0) ; } Thank you again in advance for your help. Best Regards StephQ Feb 25 '07 #1
4 Replies

 P: n/a StephQ wrote: I'm still working on the library for the simulation of SDEs. The following test code compiles fine with the digital mars compiler, but fails with gcc. Error: no matching function for call to 'prova(Diff_sine&, double, double)' #include #include "math.h" using namespace std; class Diff_sine { double theta; double gamma; double ea_Kc1, ea_Kc2; public: Diff_sine(double theta_, double gamma_); double ea_K(double x); }; inline Diff_sine::Diff_sine(double theta_, double gamma_) : theta(theta_), gamma(gamma_), ea_Kc1(theta_*gamma_/2), ea_Kc2(theta_*theta_/2) {} inline double Diff_sine::ea_K(double x) { return ea_Kc1*cos(gamma*x) + ea_Kc2*sin(gamma*x)*sin(gamma*x) ; } template double prova2(const F& theF, double a, double b) { return (theF.*f)(b); } template double prova(const F& theF, double a, double b) { double val; val = prova2(theF, a, b) ; return val; } int main() { Diff_sine theDiff_sine(1,1); double minimum ; minimum = prova(theDiff_sine, -1.0,1.0) ; } Thank you again in advance for your help. Best Regards StephQ I must confess I don't understand the code, especially the bit that says template double prova(const F& theF, double a, double b) { Isn't that const in the wrong place? Obviously not. But if you declare Diff_sine::ea_K as a const method then it compiles under g++. double ea_K(double x) const; and inline double Diff_sine::ea_K(double x) const { john Feb 25 '07 #2

 P: n/a I must confess I don't understand the code, especially the bit that says > template double prova(const F& theF, double a, double b) { Here I'm just passing a pointer to member function as a template parameter instead of a function argument. That (should) improve efficency as it (should) be easier for the compiler to inline the function call avoiding function overhead. This is the "standard" explanation, and I'm too inexperienced to comment on this :) I'm going to post a related thread in a while, maybe it could prove useful to someone else... > Isn't that const in the wrong place? Obviously not. But if you declare Diff_sine::ea_K as a const method then it compiles under g++. double ea_K(double x) const; and inline double Diff_sine::ea_K(double x) const { john You are perfectly right! Probably gcc was doing extra checks and dmc not. Thank you Regards StephQ Feb 25 '07 #3

 P: n/a On Feb 25, 2:18 pm, "StephQ" double prova(const F& theF, double a, double b) { Here I'm just passing a pointer to member function as a template parameter instead of a function argument. That (should) improve efficency as it (should) be easier for the compiler to inline the function call avoiding function overhead. This is the "standard" explanation, and I'm too inexperienced to comment on this :) I'm going to post a related thread in a while, maybe it could prove useful to someone else... Isn't that const in the wrong place? Obviously not. But if you declare Diff_sine::ea_K as a const method then it compiles under g++. double ea_K(double x) const; and inline double Diff_sine::ea_K(double x) const { john You are perfectly right! Probably gcc was doing extra checks and dmc not. Thank you Regards StephQ You should report to those who wrote the "digital mars" compiler that they have a bug. The function type must be exactly the same in the pointer to the function that you declare in the template and what you are passing in the template instantiation. In this case the const is not some extra test is a must - those are 2 different functions types and so are different pointers - it like saying that pointer to double and pointer to int are the same. You can see that in the STL function adapters there are one for none const members and const member and for a reason Feb 25 '07 #4

 P: n/a You should report to those who wrote the "digital mars" compiler that they have a bug. The function type must be exactly the same in the pointer to the function that you declare in the template and what you are passing in the template instantiation. In this case the const is not some extra test is a must - those are 2 different functions types and so are different pointers - it like saying that pointer to double and pointer to int are the same. You can see that in the STL function adapters there are one for none const members and const member and for a reason I knew that the const keyword changed the type of the declaration. And you are right in saying that it should not compile :P I will submit a bug for dmc. Thank you for your replies StephQ Feb 25 '07 #5

### This discussion thread is closed

Replies have been disabled for this discussion.