Hi,
I'm trying to use template metaprogramming to print (for debugging
purposes) the contents of an arbitrary (random-access) container. So,
for example, I'd like to be able to print a vector<int>, a vector<
vector<double> >, etc., all from the same code.
It seems like this is close to a solution, but the g++ doesn't like it
(error at bottom). Any suggestions?
template<typename T, unsigned Dim>
struct debug_print_container {
template<unsigned Dim0>
struct debug_print_container_0 {
void print(typename T::value_type item, const char *label, ostream
&out) {
debug_print_container<typename
T::value_type,Dim-1>::debug_print(item, label, out);
}
};
template<>
struct debug_print_container_0<1> {
void print(typename T::value_type item, const char *label, ostream
&out) {
out << item;
}
};
static void
debug_print(T cont, const char *label=NULL, ostream &out=cout,
const char *terminator="\n") {
if (label)
out << label << ": ";
out << "[";
for (typename T::size_type i=0; i<cont.size(); i++) {
debug_print_container_0<Dim>::print(cont[i], label, out);
if (i < cont.size() - 1)
out << ", ";
}
out << "]" << terminator;
}
};
1094: error: explicit specialization in non-namespace scope 'struct
debug_print_container<T, Dim>'
1094: error: enclosing class templates are not explicitly specialized
1095: error: template parameters not used in partial specialization:
1095: error: 'T'
1095: error: 'Dim'