[MPL/Fusion?]Call a 0-args template function on members of a sequence
Hello all,
I have the following:
template<typename T>
void f() { /* ... */ }
typedef boost::mpl::vector
Edd Dawson
Hello all,
I have the following:
template<typename T> void f() { /* ... */ }
typedef boost::mpl::vector
my_vec; I'm looking for a way to call f<T>() for all types T in my_vec. I initially hoped that either an mpl algorithm or fusion::for_each might be able to accomplish this for me, but after reading through their docs this doesn't appear to be the case.
struct call_f
{
template <class Wrapper>
void operator()(Wrapper)
{
f<typename Wrapper::type>();
}
};
mpl::for_each
David Abrahams
Edd Dawson
writes: Hello all,
I have the following:
template<typename T> void f() { /* ... */ }
typedef boost::mpl::vector
my_vec; I'm looking for a way to call f<T>() for all types T in my_vec. I initially hoped that either an mpl algorithm or fusion::for_each might be able to accomplish this for me, but after reading through their docs this doesn't appear to be the case.
struct call_f { template <class Wrapper> void operator()(Wrapper) Sorry, it's missing a "const" right here--^^^^ { f<typename Wrapper::type>(); } };
mpl::for_each
>(call_f());
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Hi Dave, Thanks for your reply. David Abrahams wrote:
David Abrahams
writes: Edd Dawson
writes: Hello all,
I have the following:
template<typename T> void f() { /* ... */ }
typedef boost::mpl::vector
my_vec; I'm looking for a way to call f<T>() for all types T in my_vec. I initially hoped that either an mpl algorithm or fusion::for_each might be able to accomplish this for me, but after reading through their docs this doesn't appear to be the case. struct call_f { template <class Wrapper> void operator()(Wrapper) Sorry, it's missing a "const" right here--^^^^ { f<typename Wrapper::type>(); } };
mpl::for_each
>(call_f());
I didn't find this in the docs. Is it meant to be there?
Unfortunately, that didn't quite work for me, anyway. A requirement of for_each
(according to my MSVC 8 diagnostics) is that each type in the sequence must be
default constructible.
The (near) exact code I'm trying to compile is:
struct register_cage
{
template<typename wrapper>
void operator()(wrapper) const
{
// ...
}
};
struct null_type { };
// template
Edd Dawson
I'm looking for a way to call f<T>() for all types T in my_vec. I initially hoped that either an mpl algorithm or fusion::for_each might be able to accomplish this for me, but after reading through their docs this doesn't appear to be the case. struct call_f { template <class Wrapper> void operator()(Wrapper) Sorry, it's missing a "const" right here--^^^^ { f<typename Wrapper::type>(); } };
mpl::for_each
>(call_f()); I didn't find this in the docs. Is it meant to be there?
The specific answer to your specific problem? No. Oh yeah, for_each wasn't documented in 1.33.1... and apparently it isn't documented in the CVS head either (still? Aleksey?)
Unfortunately, that didn't quite work for me, anyway. A requirement of for_each (according to my MSVC 8 diagnostics) is that each type in the sequence must be default constructible.
In the case above, only mpl::identity<T> needs to be
default-constructible for each type in the list... or that was my
intention Ach! My mistake...
template <class T>
struct make_identity { typedef mpl::identity<T> type; };
mpl::for_each
David Abrahams wrote:
struct call_f { template <class Wrapper> void operator()(Wrapper) Sorry, it's missing a "const" right here--^^^^ { f<typename Wrapper::type>(); } };
mpl::for_each
>(call_f()); I didn't find this in the docs. Is it meant to be there? The specific answer to your specific problem? No. Oh yeah, for_each wasn't documented in 1.33.1... and apparently it isn't documented in the CVS head either (still? Aleksey?)
Yep, I was referring to mpl::for_each in general.
Unfortunately, that didn't quite work for me, anyway. A requirement of for_each (according to my MSVC 8 diagnostics) is that each type in the sequence must be default constructible.
In the case above, only mpl::identity<T> needs to be default-constructible for each type in the list... or that was my intention Ach! My mistake...
template <class T> struct make_identity { typedef mpl::identity<T> type; };
mpl::for_each
>(call_f()); should work.
Like a charm! Many thanks! Kind Regards, Edd
participants (2)
-
David Abrahams
-
Edd Dawson