[boost] Re: MPL usage for code generation
by Andrei Alexandrescu other posts by this author
Jan 23 2003 12:25AM messages near this date
Re: [boost] MPL usage for code generation
|
Re: [boost] Re: MPL usage for code generation
"David Abrahams" <dave@[...].com> wrote in message
news:un0lswyue.fsf@[...]..
> Hugo Duncan <hugoduncan@[...].net> writes:
>
> > I am trying to use MPL to generate code.
> >
> > I have a list of types, which I would like to use to call a template
> > function with arguments that are not dependent on the list.
> >
> > eg
> >
> > given:
> > template <typename T> void my_function(std::string& s);
> > typedef list<int, float, double> my_list;
> > std::string s;
> >
> > what do I write using MPL to generate the equivalent of the following ?
> >
> > my_function<int>(s);
> > my_function<float>(s);
> > my_function<double>(s);
>
> With the latest CVS, it would be something like this:
>
> struct func
> {
> func(std::string& s) : s(s) {}
>
> template <class T>
> void operator()(T) const
> {
> my_function<T>(s);
> }
>
> private:
> std::string& s;
> };
>
> mpl::for_each<my_list>(func(s));
>
> If you have any reference or array types in my_list, you might need to
> do this instead:
>
>
> struct func
> {
> func(std::string& s) : s(s) {}
>
> template <class T>
> void operator()(mpl::identity<T>) const
> {
> my_function<T>(s);
> }
>
> private:
> std::string& s;
> };
>
> mpl::for_each<my_list, mpl::make_identity<> >(func(s));
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).
Andrei
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Thread:
Hugo Duncan
Andrei Alexandrescu
Hugo Duncan
Hugo Duncan
Hugo Duncan
David Abrahams
Peter Dimov
David Abrahams
=?iso-8859-1?Q?Terje_Sletteb=F8?=
=?iso-8859-1?Q?Terje_Sletteb=F8?=
=?iso-8859-1?Q?Terje_Sletteb=F8?=
David Abrahams
Hugo Duncan
Andrei Alexandrescu
David Abrahams
David Abrahams
Andrei Alexandrescu
David Abrahams
Douglas Paul Gregor
David Abrahams
Douglas Paul Gregor
David Abrahams
Douglas Paul Gregor
David Abrahams
Douglas Paul Gregor
|