473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with template member functions

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

Feb 25 '07 #1
4 1425
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 <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
Feb 25 '07 #2
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

Feb 25 '07 #3
On Feb 25, 2:18 pm, "StephQ" <askmeo...@mailinator.comwrote:
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: N4M | last post by:
Dear, I have codes as follows: template<class T> class A { public: // embedded class class E; public: // types typedef E TE; public:// member functions TE somefunc();
2
by: Robbie Hatley | last post by:
I've got a function that I use a lot when making utility programs that need to do the same thing to every directory in a tree. Its prototype is: unsigned long int CursDirs (void Func(void)); ...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
5
by: Michael Olea | last post by:
Here is a design problem I ran into this am - and I have cleaned the bathroom, scrubbed toilet sink and tub, windexed all glass, mopped the floor, and vacuumed the house - no dice, the problem is...
6
by: Hendrik Schober | last post by:
Hi, I have a problem with extending some existing code. In a simplified form, the problem looks like this: I have four types, A, B, C, and D. Each A refers to zero, one, or more B's and each...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
6
by: Howard | last post by:
Hi, I have a function in three unrelated but similar classes. The code in the member functions is identical for all three classes. What I want is to make a template which defines the function,...
4
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post by:
I'm having an error with this code #include <iostream> template < typename Tclass TestOpTemplate { public: friend std::ostream& operator<< <>(std::ostream& os, const TestOpTemplate<T>& m);...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.