Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-01-23 05:17:36


>From: "David Abrahams" <dave_at_[hidden]>

> > Terje Slettebø <tslettebo_at_[hidden]> writes:
> >
> > Perhaps it might be possible to do some compile-time/run-time lambda
> > (similar to Boost.Lambda for runtime, and MPL's lambda), so you could do
> > something like:
> >
> > mpl::for_each<my_list>(my_function<_>(s));
> >
> > It would then transform the function call "my_function<_>(s)" into an
> > instantiated function object of the kind suitable for mpl::for_each.
>
> I'm afraid that particular syntax won't work for this particular case,
> though. If my_function is a function template, my_function<_> is a
> function, and my_function<_>(s) calls that function.

Yes, even with function template template parameters, the syntax would have
to be changed. I realised that you can't just pass the address of a
function, since in this case, it's a function template. Besides, it wouldn't
help to use the placeholder in that place (unless you specialised the
user-supplied function, but that would be no point). The above was just to
sketch the idea of it.

> Since there are no (function template) template parameters, only
> (class template) template parameters, there doesn't appear to be any
> way to make this one particularly easy except by using the
> preprocessor to define some kind of function object.

Yes, I realised that later, as well. Is there any good reason for not
allowing function templates as template template parameters? I think it's
amazing that we got the template facility that exists, in the standard, so
maybe it simply wasn't time to consider this, to avoid delaying
standardisation.

If this was possible, you might have done something like this, instead
(using TTP):

mpl::for_each<my_list, std::string &, my_function>(s);

> It appears to be just bad luck that higher order functional
> programming with function templates is impossible in C++.

Well, it might be changed in a future revision of the standard, if it turns
out to be practical.

I can imagine something like this:

template<class Sequence, class T, template<class> void Function(T)>
struct for_each;

Possibly using overloaded class templates, as well (another possible
extension). Anyway, this goes beyond the topic of Boost.

Regards,

Terje


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk