mpl::for_each and polymorphic function object

Hi, I found following issue: if I have const type in a list of types test case template facility initialized with, it really gets instantiated with non const type. After some investigation I found the reason. The reason is the mpl::for_each, used in the facility implementation, rely on type deduction based on type of single argument it passes. It works for simple function and function object. But it does not for function object with template operator(). It's all originated from the fact that following code compiles: struct { template<typename T> void operator()( T ) { T t = 0; ++t; } } foo; int main() { char const x = 0; foo( x ); return 0; } because foo.operator() is instantiated with type char - not char const. Is the any workarounds? Could we fix that? Gennadiy. P.S. Actually I was concerned with this unused parameter from the very beginning. It prohibit using for_each with types without default constructor and also could be source for performance problems. Could we eliminate this unused parameter at all?

"Gennadiy Rozental" <gennadiy.rozental@thomson.com> writes:
Hi,
I found following issue: if I have const type in a list of types test case template facility initialized with, it really gets instantiated with non const type. After some investigation I found the reason. The reason is the mpl::for_each, used in the facility implementation, rely on type deduction based on type of single argument it passes. It works for simple function and function object. But it does not for function object with template operator(). It's all originated from the fact that following code compiles:
struct { template<typename T> void operator()( T ) { T t = 0; ++t; } } foo;
int main() { char const x = 0;
foo( x );
return 0; }
because foo.operator() is instantiated with type char - not char const.
Is the any workarounds? Could we fix that?
template <class T> struct wrap {}; typedef mpl::vector<int const, long&, char*&> s; struct foo { template<typename T> void operator()( wrap<T> ) { T t = 0; ++t; } }; mpl::for_each<mpl::transform<s, wrap<_1> >::type>(foo()); -- Dave Abrahams Boost Consulting www.boost-consulting.com

mpl::for_each<mpl::transform<s, wrap<_1> >::type>(foo());
I saw this in mpl::for_each test. It may probably solve my problem. But is tyhe more generic solution? Or why doesn't for each pach mpl::identity in a first place? Or could we eliminate parameter at all and make for_each to work with explicit type specialization, like this: aux::unwrap(f, 0)::operator()<arg>(); Gennadiy.
participants (2)
-
David Abrahams
-
Gennadiy Rozental