Metaprogamming question

I roughed out a function that will be a template that dispatches to one of several different forms based on the nature of one of the arguments. It's fairly simple, each helper call asking one question to further divide it up. But, I want to write a dozen functions that all have similar (if not identical) issues. So I want to abstract out the mixmaster and code that once. I'm thinking that I map the argument to a "tag" that has many possible values, and have one set of overloaded functions. It is easy to write the back end, as each function's tag is clear what that case is for. Furthermore, I can make the tags empty structs that use an inheritance pattern, so any forms I don't supply for some function will simply pick the more general one. So, how do I write the metafunction that maps the type to the tag? I could follow my same approach of one step at a time. But I wonder if a "switch" statement of some kind would be clearer and easier. OTOH, I don't want to learn all of MPL just for one thing, and I don't want to bog down the compilation speed. Is there something specific someone can point me to? Second, my pseudocode has a metafunction for telling me whether a type is a resizable container. STL containers predate the modern style of metafunctions and use cumbersome traits instead, but is there something around that I can use to tell that something is supposed to be a STL container or specific categories of containers? I could just list them all one by one, but type_traits already did that for me for a lot of things, and I'd have to tell everyone how to meta-annotate their own classes that are supposed to work "like" STL containers for algorithms. Can I use something like enable_if to tell if a class has a push_back or resize function? Thanks everyone, --John

El 08/07/2011 12:17, John M. Dlugosz escribió:
I roughed out a function that will be a template that dispatches to one of several different forms based on the nature of one of the arguments. It's fairly simple, each helper call asking one question to further divide it up.
But, I want to write a dozen functions that all have similar (if not identical) issues. So I want to abstract out the mixmaster and code that once. I'm thinking that I map the argument to a "tag" that has many possible values, and have one set of overloaded functions. It is easy to write the back end, as each function's tag is clear what that case is for. Furthermore, I can make the tags empty structs that use an inheritance pattern, so any forms I don't supply for some function will simply pick the more general one.
So, how do I write the metafunction that maps the type to the tag?
Using a boost::mpl::map? http://www.boost.org/libs/mpl/doc/refmanual/map.html Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Este mensaje se dirige exclusivamente a su destinatario. Puede consultar nuestra política de envío y recepción de correo electrónico en el enlace situado más abajo. This message is intended exclusively for its addressee. We only send and receive email on the basis of the terms set out at. http://www.tid.es/ES/PAGINAS/disclaimer.aspx

On 08/07/2011 12:30, joaquin@tid.es wrote:
So, how do I write the metafunction that maps the type to the tag?
Using a boost::mpl::map?
Or explicit specialization.

On 7/8/2011 8:17 PM, Nat Goodspeed wrote:
On Jul 8, 2011, at 6:17 AM, "John M. Dlugosz"
wrote: Can I use something like enable_if to tell if a class has a push_back or resize function?
Isn't that what the Type Traits Introspection library (currently under review) does?
Yes, precisely. As long as you know the signature of the member function or static member function for which you are looking, TTI can tell you at compile-time whether it exists or not. I encourage you to look at the library and give your comments as well as review the library.
participants (5)
-
Edward Diener
-
joaquin@tid.es
-
John M. Dlugosz
-
Mathias Gaunard
-
Nat Goodspeed