473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

attempting template metaprog to print arbitrary container

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'

May 12 '06 #1
2 1520
tu*****@gmail.com 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
May 13 '06 #2
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.

May 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark P | last post by:
I'm trying to define a templatized version of operator<< to print out information about a template class. The code below won't compile. (error: no match for std::ostream& << Outer<int>::Inner&) ...
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
4
by: Dmytro | last post by:
Hi! This code is ok: template<class T> struct Container { T x; typedef int size_type; size_type size() { return sizeof T; }
4
by: Fei Liu | last post by:
#include <vector> #include <iostream> template <template <typename T, typename Allocclass C> struct A{ //C<Tc; //A(){ c=10; } void print(){ std::cout << "A" << std::endl; } };
35
by: Steven T. Hatton | last post by:
Perhaps I'm just a bit frustrated, and I will soon realize the clear truth of the matter, but right now I have some serious misgivings about the value of investing a lot of time and effort into...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
7
by: aaragon | last post by:
Hi everyone, The idea is quite simple: generate a container with random values in it. For that, I decided to create a class that I called RandomContainer that inherits from a container (with...
1
by: AS | last post by:
Can anybody explain what is a "Template Specialization" and how it is useful and what is the use of "typename" Thanks AS
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.