473,411 Members | 1,980 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,411 software developers and data experts.

Interface problem

I'm writing some algorithms that works on generic functions using
boost::bind.
My problem is that class templates can never be deduced.

In the simplest cases I just write the class like:

class Legendre
{
....
public:
template<class F>
static double computeIntegral( const F& f );
}

double value = Legendre::computeIntegral(boost::bind( &Sde::drift,
&sde, _1 ));

However in more complext examples, I have to work with classes with
non static memeber fucntions.
For example becouse I need to store data previously calculated about
the function for efficecny reasons.
I would like to "fix" the F at the moment of the construction of the
class and then invoke member functions of the class to perform
operations.

That is I would like to being able to do something like:

template<class F>
class FunctionAnalysis
{
private:

F& function;

public:
FunctionAnalysis( const F& f );

double operation1();
double operation2();
}

template<class F>
FunctionAnalysis<F>::FunctionAnalysis( const F& f)
:
function(f)
{}

FunctionAnalysis driftAnalysis(boost::bind( &Sde::drift, &sde, _1 ));
double x = driftAnalysis.operation1();
double y = driftAnalysis.operation2();

There are two problems:
1) I class template arguments can never be deduced
2) driftAnalysis is not of type FunctionAnalysis

I can solve the fist problem using the same approach of make_pair, but
I dont't see any way to solve th second problem.

The objective is to avoid having to specify the template parameter of
the class (and using boos::bind you can easily see why :D ).

Do you have any suggestion?

Thank you!

Cheers
StephQ

Jun 4 '07 #1
3 3526
Problem 1.
Only template functions can deduce types from an argument list. This
excludes C++ constructors!
The common solution for this is a make_function() template function.
Example:

template<typename T>
FunctionAnalysis<Tmake_analysis(const T& func)
{
return FunctionAnalysis<T>(func);
}

Problem 2.
You are using a template with out template parameters. It needs to be
some thing more like this

typedef boost::bind( &Sde::drift, &sde, _1 )::type;
FunctionAnalysis<TdriftAnalysis(boost::bind( &Sde::drift, &sde,
_1 ));

But this is no fun.
You should really not pass boost::bind types into template parameters
like this. Here is what I would propose.

Alternate Solution

class FunctionAnalysis
{
private:
boost::function<double(double)function;

public:
FunctionAnalysis(boost::function<double(double)f );
double operation1();
double operation2();
}

// use it like this without any trouble

FunctionAnalysis driftAnalysis(boost::bind( &Sde::drift, &sde, _1 ));
double x = driftAnalysis.operation1();
double y = driftAnalysis.operation2();

On Jun 4, 11:10 am, StephQ <askmeo...@mailinator.comwrote:
I'm writing some algorithms that works on generic functions using
boost::bind.
My problem is that class templates can never be deduced.

In the simplest cases I just write the class like:

class Legendre
{
...
public:
template<class F>
static double computeIntegral( const F& f );

}

double value = Legendre::computeIntegral(boost::bind( &Sde::drift,
&sde, _1 ));

However in more complext examples, I have to work with classes with
non static memeber fucntions.
For example becouse I need to store data previously calculated about
the function for efficecny reasons.
I would like to "fix" the F at the moment of the construction of the
class and then invoke member functions of the class to perform
operations.

That is I would like to being able to do something like:

template<class F>
class FunctionAnalysis
{
private:

F& function;

public:
FunctionAnalysis( const F& f );

double operation1();
double operation2();

}

template<class F>
FunctionAnalysis<F>::FunctionAnalysis( const F& f)
:
function(f)
{}

FunctionAnalysis driftAnalysis(boost::bind( &Sde::drift, &sde, _1 ));
double x = driftAnalysis.operation1();
double y = driftAnalysis.operation2();

There are two problems:
1) I class template arguments can never be deduced
2) driftAnalysis is not of type FunctionAnalysis

I can solve the fist problem using the same approach of make_pair, but
I dont't see any way to solve th second problem.

The objective is to avoid having to specify the template parameter of
the class (and using boos::bind you can easily see why :D ).

Do you have any suggestion?

Thank you!

Cheers
StephQ

Jun 5 '07 #2
Only template functions can deduce types from an argument list. This
excludes C++ constructors!
The common solution for this is a make_function() template function.
Example:

template<typename T>
FunctionAnalysis<Tmake_analysis(const T& func)
{
return FunctionAnalysis<T>(func);

}
Eaxctly what I was saying abaout make_pair.
Problem 2.
You are using a template with out template parameters. It needs to be
some thing more like this

typedef boost::bind( &Sde::drift, &sde, _1 )::type;
FunctionAnalysis<TdriftAnalysis(boost::bind( &Sde::drift, &sde,
_1 ));

But this is no fun.
I agree :)
You should really not pass boost::bind types into template parameters
like this. Here is what I would propose.

Alternate Solution

class FunctionAnalysis
{
private:
boost::function<double(double)function;

public:
FunctionAnalysis(boost::function<double(double)f );
double operation1();
double operation2();

}

// use it like this without any trouble

FunctionAnalysis driftAnalysis(boost::bind( &Sde::drift, &sde, _1 ));
double x = driftAnalysis.operation1();
double y = driftAnalysis.operation2();
Thank you very much, it's excactly what I was searching for.
I just ignored the existance of boost::function.

Cheers
StephQ

Jun 5 '07 #3
I just run in the following problem.
I'm using the gsl library that requires an input function to be of the
form:

struct gslFunction
{
double (*func)(double x, void* p) function;
void* params;
}
To be able to interface to the gsl I wrote this "converter" (based on
root's mathmore library gsl wrapper):
// Use in combination with boost::bind.
template<class F>
static double gslFunctionAdapter( double x, void* p)
{
// Here I do recover the "right" pointer, safer to use static_cast
than reinterpret_cast.
F* function = static_cast<F*>( p );
return (*function)( x );
}

template<class F>
gsl_function convertToGslFunction( const F& f )
{
gsl_function gslFunction;

const void* p = &f;
assert (p != 0);

gslFunction.function = &gslFunctionAdapter<F>;
gslFunction.params = const_cast<void*>( p ); // Just to eliminate
the const.

return gslFunction;
}

and use this like:
gslFunction gslF = convertToGslFunction( boost::bind( &Sde::drift,
&sde, _1 ) );

However the problem is that now I'm using boost::function in my
algorithms, following your suggestion.
So in an algorithm I get into the situation:

boost::function<double (double)f = boost::bind( &Sde::drift, &sde,
_1 );
gslFunction gslF = convertToGslFunction( f );

I know that this "double-wrapper" is bad for efficency, but I can't
see any other way to use the gsl library without modification on the
gsl library itself.
The problem is that this does not work! When I call the gslF the
software crash.
Debugger inspection reveals that the line:

F* function = static_cast<F*>( p ); // in gslFunctionAdapeter

fails to "recover" my boost::funcion.

Is there anything I could do to solve the situation?

The two requirements are:

1) Be able to implement the approach suggested in the post above: that
is have boost::function private member data in my algorithms

2) Interface to the gslFunction struct without any modification to the
gsl library.

Thank you again for your help.

Best Regards
StephQ


Jun 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Davide M3xican Coppola | last post by:
Hi, I would realize an interface (interface) for a pair of concrete classes (concrete & concrete2), but I have a problem because I have to access to a third class (element) and if I try to declare...
3
by: Pol Bawin | last post by:
A class has a private field of type IWizard (An interface) and a public property to access it. When I try to serialize the Geometry class in XML, i have an error but it works in Binary Can...
7
by: Stephan Rose | last post by:
Ok here is my scenario I have an interface called IScalar which describes a one dimensional number that has a certain unit of measurement. This interface is used to create multiple structures,...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
5
by: LS | last post by:
Can a WebMethod return an Interface type? Can we pass an interface parameter ? Example : public interface IEntity { long Id { get; set; } string Name { get; set; } }
12
by: masoud bayan | last post by:
I've come across something in Interface implementation that I am not sure is correct behavior in VB.NET (and maybe C#) or not? Consider following example: Public Interface IShape
2
by: stefan.moser | last post by:
Hi All, I'm having a problem implementing the Separated Interface pattern from Martin Fowler's book Patterns of Enterprise Application Architecture. I'm using C# in Visual Studio 2005. The...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
7
by: WTH | last post by:
I am now aware (I am primarily a C++ developer) that in C# if you reference the same interface from the same file in two different projects the types are actually incompatible. I found this out...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.