Problem with template member functions

S

StephQ

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 <iostream>
#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<class F, double (F::*f)(double) const>
double prova2(const F& theF, double a, double b) {
return (theF.*f)(b);
}

template<class F, double (F::*f)(double) const>
double prova(const F& theF, double a, double b) {
double val;
val = prova2<F, f>(theF, a, b) ;
return val;
}


int main() {
Diff_sine theDiff_sine(1,1);

double minimum ;

minimum = prova<Diff_sine, &Diff_sine::ea_K>(theDiff_sine,
-1.0,1.0) ;

}


Thank you again in advance for your help.

Best Regards
StephQ
 
J

John Harrison

StephQ said:
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 <iostream>
#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<class F, double (F::*f)(double) const>
double prova2(const F& theF, double a, double b) {
return (theF.*f)(b);
}

template<class F, double (F::*f)(double) const>
double prova(const F& theF, double a, double b) {
double val;
val = prova2<F, f>(theF, a, b) ;
return val;
}


int main() {
Diff_sine theDiff_sine(1,1);

double minimum ;

minimum = prova<Diff_sine, &Diff_sine::ea_K>(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<class F, double (F::*f)(double) const>
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
 
S

StephQ

I must confess I don't understand the code, especially the bit that says
template<class F, double (F::*f)(double) const>
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
 
B

boaz1sade

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...







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
 
S

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top