Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > attempting template metaprog to print arbitrary container

Reply
Thread Tools

attempting template metaprog to print arbitrary container

 
 
tutufan@gmail.com
Guest
Posts: n/a
 
      05-12-2006
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>:rint(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'

 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      05-13-2006
wrote:
> 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.

--
Alan Johnson
 
Reply With Quote
 
 
 
 
tutufan@gmail.com
Guest
Posts: n/a
 
      05-13-2006
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_containe r_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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
attempting to print unicode characters. Ray C Programming 23 08-30-2010 01:53 PM
Developing Rails plugin: need some metaprog. help Joshua Muheim Ruby 4 11-26-2007 09:50 AM
Attempting to Cancel a Print Job John Computer Support 8 10-07-2005 05:01 AM
calling an arbitrary function w/ arbitrary arguments Honestmath C++ 5 12-13-2004 06:18 AM
how to write a class that stores things in arbitrary stl container 3doutpost C++ 2 12-05-2004 07:17 AM



Advertisments
 



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 52 53 54 55 56 57