| navderm (93) | |
|
Hi All, it might be a weird question but I wanted to know if I can have a class be templated on variable no. of datatypes. Like template < class A, class B ... > class PrintSTL { // depending on how many datatypes are provided, I can print either just a vector, or a map , or a multimap etc . } Is there a way ?? Thanks a bunch. ~Neeraj | |
|
|
|
| coder777 (2378) | |
|
I don't think so. The complexity to handle this different kind of container is usually greater than the gain. You can determine the number of types: http://en.wikipedia.org/wiki/Variadic_template But since it's not for preprocessor you cannot change the container type depending on this. You could pass the container type as a template parameter though. | |
|
|
|
| Peter87 (3691) | |
| Why use a class to print containers? Why not simply use a function? | |
|
|
|
| kbw (5375) | |||
That's why iterators across all the STL containers look alike, so you can write code that works across them. For example:
| |||
|
|
|||
| JLBorges (1336) | ||||
|
> depending on how many datatypes are provided, I can print either just a vector, or a map , or a multimap etc . > Is there a way ?? There is. Is it worth the trouble?
Output:
| ||||
|
|
||||
| Cubbi (1583) | |
| Taking it a bit further, this becomes a library: http://louisdx.github.com/cxx-prettyprint/ | |
|
|
|