   type names as function parameters [SOLVED]
  hey again,just a quick one this time: i've been looking around and haven't been able to discover whether there is a way to pass type names as parameters to functions and then use them (say in type checking or template-class-instance creation).something along the lines of:void MyFunc (type type_var);and then using "type_var" to assign things like so:temp_class<type_var> temp_obj;like i said i've looked around but haven't found a way (if there is one) that i can do this sort of type parameter passing.thanks for any help![Edited by - the_moo on May 15, 2006 3:47:31 AM]
  look up "templates"   ;  That set of angle-brackets says he IS using templates. Duh.And, no, you really can't. You can encapsulate type information, using the built in type_info structure or a bit of polymorphism, and then use that in a factory, but you'll never get that perfect syntax you want because types aren't first-class objects.   ;  Surely it is possible with a simple function template:template <typename T>void function(){  Foo<T> foo;}function<int>();function<double>();function<Bar>();;  Look into the typeid operator. It gives you an object (of type type_info) that can be used to perform comparisons and things like that; that's really the best you can do.However, you can't do what you're proposing in your first post - using a variable as a template parameter - because template parameters are resolved at compile time. MyType<int> and MyType<float> are two entirely seperate types, related only by the fact that they were generated from the same template (MyType<>) through what is more or less a search-and-replace operation. If you need this kind of variable switching, you've got two options. The first do what Sharlin says, and make MyFunc templated as well. This should work for any type, though it means that you'll be generating a seperate copy of MyFunc for each and every type you use it on.The second approach would be to use type_info and a std::map or something... but it's not very nice. It's probably not suitable to anything beyond a small range of situations.   ;  also you could, for instance, instanciate the type you want to pass, and have the function not templated but using several overloads. basically a type-based tag dispatching. Of course this should only be used if your types 'are' tags.. i.e. empty structs. struct do_this {};struct do_that {};void do_something(do_this &) { /* do something useful */ }void do_something(do_that &) { /* do something more useful */ }template void this_will_execute_one_of_the_do_functions{ do_something(DoWhat()); }// somewhere in a function body: this_will_execute_one_of_the_do_functions();dinkumware's stl implementation makes use of this in quite a number of places. But again, only use this if your structs are empty or your constructor's empty at least. a decent compiler might optimize out the instantiation. it's commonly only used for empty tag-like structs, though. Cheers,Simon;  brackets of the template function got cut by the forum. of course there should be a class and a class name "DoWhat" as the function's template argument..-simon   ;  I interpretted his original question to mean he wanted to do something more than just have a template function. Suppose I shouldn't assume that since he's using template classes he knows he can template functions too. :/   ;Quote:Original post by Anonymous Posteralso you could, for instance, instanciate the type you want to pass, and have the function not templated but using several overloads.To be fair, that's pretty much what templates do internally... but you're completely right, and if the implementation of the function is going to be quite different for each type, then it may be the best approach. (The 'middle ground' is explicit template specialization).   ;  thanks Sharlin and superpig [smile]. i must confess i didn't realise that functions could also be templated. i've used a mix of that and typeid to work out the problem.thanks again for all the help,   