selecting a type (tag) based on a runtime enum value

Hello, I have a templated function Engine, that takes 3 template arguments, these are tags T1, T2, T3, T4. I have an typedef enum { e1, e2, e3, e4 } E to element ei corresponds the type Ti ... const E e // switch (e) { case e1: Engine< Metafct1<T1>::type, Metafct2<T1>::type, Metafct3<T1>::type >::Process( .... ); break; case e2: Engine< Metafct1<T2>::type, Metafct2<T2>::type, Metafct3<T2>::type >::Process( .... ); break; ... } Is there a shorter way of writing this... For now I only have 4 in the enum, but I may have more later? regards,

Not really. You've essentially got a map from ints to function calls. You could implement it with an array of fps, a map container of some kind, a switch() statement as you've done... and so on. There are so many way to implement it, and they're all more or less the same. I'd choose the most readable, maintainable, debuggable, and which makes the most use of the compiler to enforce constraints. Using that criteria, the switch is actually a good candidate. (Helpful tip: Don't include a `default` case, and enable GCC's -Wswitch or your compiler's equivalent warning). Hicham Mouline wrote:
Hello,
I have a templated function Engine, that takes 3 template arguments, these are tags T1, T2, T3, T4.
I have an typedef enum { e1, e2, e3, e4 } E to element ei corresponds the type Ti
... const E e // switch (e) { case e1: Engine< Metafct1<T1>::type, Metafct2<T1>::type, Metafct3<T1>::type >::Process( .... ); break; case e2: Engine< Metafct1<T2>::type, Metafct2<T2>::type, Metafct3<T2>::type >::Process( .... ); break; ... }
Is there a shorter way of writing this... For now I only have 4 in the enum, but I may have more later?
regards, _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I may add that if the OP fears it may go out of hand for large enum, hecan use preprocessing to generate the switch body -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35

For short notation you can use also BOOST_PP_ macroses
On Mon, Mar 16, 2009 at 8:07 PM, Joel Falcou
I may add that if the OP fears it may go out of hand for large enum, hecan use preprocessing to generate the switch body
-- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Hicham Mouline
-
Joel Falcou
-
John
-
Maxim Koshelev