Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-01-22 22:22:58


Terje Slettebø <tslettebo_at_[hidden]> writes:

>>From: "David Abrahams" <dave_at_[hidden]>
>
>> "Andrei Alexandrescu" <andrewalex_at_[hidden]> writes:
>>
>> > Or (barring my lack of mastering some syntactic details):
>> >
>> > inline void do_my_function(string&, void_) {}
>> >
>> > template <class Lst>
>> > inline void do_my_function(string& s, Lst lst)
>> > {
>> > my_function<front<Lst>::type>(s);
>> > do_my_function(s, pop_front<Lst>::type());
>> > }
>> > ...
>> > do_my_function(s, my_list());
>> >
>> > No struct, no for_each, no operator(). Et que le meilleur gagne :o).
>>
>> Yeah, I haven't always been very comfortable with the
>> value-per-character of mpl::for_each -- it seems to have similar
>> properties to std::for_each in that way.
>
> 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.

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.

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

-- 
                       David Abrahams
   dave_at_[hidden] * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

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