variable datatypes in templating

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
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.
Why use a class to print containers? Why not simply use a function?
That's why iterators across all the STL containers look alike, so you can write code that works across them. For example:
1
2
3
4
5
6
7
8
9
template <typename Container>
std::ostream& print(std::ostream &os, const Container &c)
{
    static const std::string delimiter = " ";
    for (Container::const_iterator p = c.begin(); p != c.end(); ++p)
        os << *p << delimiter;

    return os;
}
> 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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

namespace impl
{
    template < typename T > inline void do_print( const T& v, std::ostream& stm )
    { stm << v << ' ' ; }

    template < typename F, typename S > // print pair
    inline void do_print( const std::pair<F,S>& p, std::ostream& stm )
    { stm << '{' << p.first << ',' << p.second << "} " ; }
}

template < typename... ARGS, template < typename... ARGS > class SEQ  > // print for standard contaners
void print( const SEQ<ARGS...>& seq, std::ostream& stm = std::cout,
             typename SEQ<ARGS...>::iterator* = 0  )
{ stm << "[ " ; for( const auto& v : seq ) impl::do_print(v,stm) ; stm << "]\n" ; }

// overload for C-style array
template < typename T, std::size_t N >
void print( T (&seq)[N], std::ostream& stm = std::cout )
{ stm << "[ " ; for( const auto& v : seq ) impl::do_print(v,stm) ; stm << "]\n" ; }

#include <array> // overload for std::array
template < typename T, std::size_t N >
void print( const std::array<T,N>& seq, std::ostream& stm = std::cout )
{ stm << "[ " ; for( const auto& v : seq ) impl::do_print(v,stm) ; stm << "]\n" ; }

#include <vector>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>

int main()
{
    { int seq[] { 1, 2, 3, 4 } ; print(seq) ; }
    { std::array<int,4> seq{ { 1, 2, 3, 4 } } ; print(seq) ; }
    { std::vector<int> seq{ 1, 2, 3, 4 } ; print(seq) ; }
    { std::deque<int> seq{ 1, 2, 3, 4 } ; print(seq) ; }
    { std::list<int> seq{ 1, 2, 3, 4 } ; print(seq) ; }
    { std::set<int> seq{ 1, 2, 3, 4 } ; print(seq) ; }
    { std::multiset<int> seq{ 1, 2, 3, 1, 2, 4 } ; print(seq) ; }
    { std::unordered_set<int> seq{ 1, 2, 3, 4 } ; print(seq) ; }
    { std::unordered_multiset<int> seq{ 1, 2, 3, 1, 2, 4 } ; print(seq) ; }
    { std::map<int,int> seq{ {1, 0}, {2, 3}, {4, 5} } ; print(seq) ; }
    { std::multimap<int,int> seq{ {1, 0}, {2, 3}, {4, 5}, {2,8} } ; print(seq) ; }
    { std::unordered_map<int,int> seq{ {1, 0}, {2, 3}, {4, 5} } ; print(seq) ; }
    { std::unordered_multimap<int,int> seq{ {1, 0}, {2, 3}, {4, 5}, {2,8} } ; print(seq) ; }
}

Output:
[ 1 2 3 4 ]
[ 1 2 3 4 ]
[ 1 2 3 4 ]
[ 1 2 3 4 ]
[ 1 2 3 4 ]
[ 1 2 3 4 ]
[ 1 1 2 2 3 4 ]
[ 4 3 2 1 ]
[ 4 3 2 2 1 1 ]
[ {1,0} {2,3} {4,5} ]
[ {1,0} {2,3} {2,8} {4,5} ]
[ {4,5} {1,0} {2,3} ]
[ {4,5} {2,8} {2,3} {1,0} ]


Taking it a bit further, this becomes a library: http://louisdx.github.com/cxx-prettyprint/
Topic archived. No new replies allowed.