|
Boost Users : |
Subject: Re: [Boost-users] Array of types to change template function in a for loop
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-09-12 11:46:27
AMDG
Patrick Loney wrote:
> I've got some code which needs to call a template function on different
> objects (potentially in a for loop). I'm currently doing this using an
> array of function ptrs but like the idea of an array of types.
> <snip>
>
> I believe this should be possible using the pre-processor or meta
> programming parts of boost but have no experience of either and was
> hoping for some pointers. Is it possible and is it, IYO, better than the
> function pointer method I'm currently using?
>
boost::mpl::for_each does what you want
http://www.boost.org/doc/libs/1_36_0/libs/mpl/doc/refmanual/for-each.html
(untested):
template<class T>
struct wrap {};
struct func_f {
typedef void result_type;
template<class T>
void operator()(const wrap<T>&, const std:;string& s) const {
func<T>(s);
}
};
int main() {
boost::mpl::for_each<boost::mpl::vector<Obj1, Obj2, Obj3>,
wrap<boost::mpl::_1> >(
boost::bind(func_f(), _1, "testid"));
}
In Christ,
Steven Watanabe
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net