attempting template metaprog to print arbitrary container

T

tutufan

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, 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'
 
A

Alan Johnson

It seems like this is close to a solution, but the g++ doesn't like it
(error at bottom). Any suggestions? [snip]
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'

You are trying to explicitly specialize a member template without
explicitly specializing its enclosing templates, which is not allowed.
See 14.7.3.18 of the standard:

"In an explicit specialization declaration for a member of a class
template or a member template that appears in namespace scope, the
member template and some of its enclosing class templates may remain
unspecialized, except that the declaration shall not explicitly
specialize a class member template if its enclosing class templates are
not explicitly specialized as well."

There is probably a good reason why such a thing is not allowed, but I
don't know it.
 
T

tutufan

That's kind of what I thought. It seems like the problem would be
solved if I could just explicitly specialize the outer template, but I
don't see how to do that. I tried doing something like

template<>
struct debug_print_container<T,Dim>::debug_print_container_0<1> {

but it didn't help. Any thoughts?

This seems like the kind of function that'd be generally useful, so I
wonder if someone hasn't already done it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top