RE: OON: Re: oon-digest V1 #38

From: Geoffrey Furnish (furnish@actel.com)
Date: Fri Mar 26 1999 - 23:52:39 EST


Michael W. Buksas writes:
> Could you elaborate on the use of the function_trait class here?
>
> > template<typename T, typename Function>
> > typename function_trait<Function>::value_type
> > integrate(const T& start, const T& end, const Function& func)
> > { /* ... */ }
>
> It's the second line which is confusing to me.

The idea is that you need to be able to know what type of value the
function evaluates to. If the argument func of type Function happened
to be an actual global function pointer, then you wouldn't be able to
extract types like this from it directly via "Function::value_type",
for example. That's the reason for the "external polymorphism" which
was suggested in the above, and is used all over the STL. It sounds
like you understand that, but I thought I should make it explicit in
case anyone else was mystified.

> I understand that it
> is using a trait class to define the return type of the integrate
> function, but where is the class function_trait defined?

The author of integrate probably has something like this:

% cat integrate.hh
template<class F> class function_traits {};

<prototype of integrate, or perhaps whole function definition,
depending on intended instantiation model>

> Is this a built in part of a library (such as the STL)?

Well, "function_traits" isn't in the STL, but if integrate is provided
in a library, then some empty template class declaration of
function_traits is probably provided there.

> Or, would the implementor of the integrate function also write this
> trait class, and provide template specializations for the desired
> function types?

Well, if we assume the author of integrate is a different person than
the user of integrate, then the usage model would probably be
something like this:

Author of integrate provides integrate.hh as exhibited above.

Application includes integrate.hh, declares his function to integrate,
specializes function_traits on this type, then calls integrate. It
might look like:

% cat application.cc
#include "integrate.hh"

class MyFunc {
public:
    double operator()( double x ) { return exp(x)*sin(x); }
};

template<> class function_traits<MyFunc> {
public:
    typedef double value_type;
};

void compute()
{
    double answer = integrate( 2., 4., MyFunc() );
    cout << "answer = " << answer << endl;
}

> I hope nobody minds what is probably an elementary question, but this
> technique would be of great help to me too.

<Shameless plug> I wrote an article in CIP last spring which
discusses issues related to this. "Container Free Numerical
Algorithms in C++", CIP, May/June, 1998. It might be useful for
additional orientation.

Good luck,

-- 
Geoffrey Furnish              Actel Corporation         furnish@actel.com
Senior Staff Engineer        955 East Arques Ave        voice: 408-522-7528
Placement & Routing       Sunnyvale, CA   94086-4533    fax:   408-328-2303



This archive was generated by hypermail 2b29 : Wed Feb 20 2002 - 03:20:08 EST