|
Boost : |
From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-01-24 06:19:57
>From: "Aleksey Gurtovoy" <agurtovoy_at_[hidden]>
> David Abrahams wrote:
> >
> > It appears to be just bad luck that higher order functional
> > programming with function templates is impossible in C++.
>
> My current understanding (which, admittedly, is not backed up by a
> real-world experience) is that if you care about higher-orderness of your
> generic algorithms, a preferred implementation construct for those
> algorithms is not a function template, but a static _function object_ (a
> technique used in FC++):
>
> struct my_function_
> {
> template< typename U >
> void operator()(std::string const& text, U)
> {
> // ...
> }
>
> } my_function; // here!
>
>
> For ordinary uses, the above will act just like a plain function template
> (minus ADL/explicit template arguments specification):
However, it does tend to create some boilerplate code, if all you want is to
call a function template for all the types. It's similar to std::for_each,
if you want to execute arbitrary code, unless you use BLL.
Nevertheless, I've asked at comp.std.c++ why function template template
parameters aren't allowed (the posting hasn't appeared, yet).
> and it will also allow one to do something like this:
>
> std::string text("text");
> mpl::for_each< my_types >(boost::bind<void>(my_function, text, _1));
I got "error: no instance of function template "boost::mpl::for_each"
matches the argument list", when trying this on Intel C++. Do you have a
version of this that works?
Regards,
Terje
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk