|
Boost : |
From: Neal Becker (ndbecker2_at_[hidden])
Date: 2006-03-25 23:06:18
One tricky area is the need to infer the types of functions, for example,
when functions are composed. Various approaches have been used, but the
most flexible approaches are those that are not intrusive in the sense of
not imposing restrictions on the functions being composed. Fortunately, I
think we now can use boost::result_of to help.
I noticed that in c2 that compose was lacking gerericity (is that a word?).
Here is a demonstration that I think you may find helpful.
,----[ /home/nbecker/Test.cc ]
| #include <boost/utility/result_of.hpp>
| #include <cmath>
|
| template<typename T>
| struct Sqr {
| T operator () (T x) { return x * x; }
| };
|
| template<typename T>
| struct Sin {
| T operator () (T x) { return ::sin (x); }
| };
|
| template<typename F_type, typename G_type>
| struct Compose {
| F_type f;
| G_type g;
|
| Compose (F_type _f, G_type _g) : f (_f), g (_g) {}
|
| template<typename arg_t>
| typename boost::result_of<G_type (F_type (arg_t))>::type operator()
| (arg_t x) {
| return g (f (x));
| }
|
| };
|
| int main() {
| Compose<Sqr<double>,Sin<double> > C (Sqr<double>(), Sin<double>());
| }
`----
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk