Template specialization, nested

Y

Your Name

Hi,

I use templated functions to handle "ToString" functionality. For
example:


template<> void ToString<int>(std::string &s, int &value)
{
s = str(boost::format("%1$d") % value);
}

template<> void ToString<float>(std::string &s, float &value)
{
s = str(boost::format("%1$1.7g") % value);
}

// ... lots more specialization ...


Now I'd like to add a version of ToString for std::vector. But all I
really want to do is call the corresponding ToString<element_type> where
"element_type" is the type of element in the vector. Is it possible to
handle this with a single ToString function, or do I have to write one
for every possible type of vector element? It seems like a waste, since
the code in every function would be identical except for what's inside
the <> brackets.

Here's kind of a psuedo-code for what I want, but I don't know how to
make the syntax work...


// Create a string of the form "[1, 2, 3, 4, 5]"
template<>
void ToString<std::vector<element_type>>(std::string &s,
std::vector<element_type> &value)
{
s = "[";

std::vector<element_type>::iterator i = value.begin();

while (i != value.end())
{
std::string temp;
ToString<element_type>(temp, *i);

i++;

s += temp;

if (i != value.end())
{
s += ", ";
}
}

s += "]";
}


Hope the example makes sense. Thanks.

Pat
 
Y

Your Name

Pete Becker said:
Seems like you need a partial specialization:

template <class element_type>
void ToString<std::vector<element_type>>(std:string &s,
std::vector<element_type> &value)



I get the following compile error (Visual Studio 2005):

error C2768: 'ToString' : illegal use of explicit template arguments

I tried changing the "class" keyword to "typename" and the error doesn't
change. I also tried removing the code from the function and even an empty
stub generates the error message, so it's the function declaration that the
compiler doesn't like...
 
F

Frank Birbacher

Hi!

Pete said:
template <class element_type>
void ToString<std::vector<element_type>>(std:string &s,
std::vector<element_type> &value)

Watch out: the ">>" must be separated by whitespace, like "> >".

Frank
 
Y

Your Name

Pete Becker said:
....

Since you haven't given any context, it's impossible to guess what the
real problem is.


I don't know what context you're looking for, exactly... I just tried to
compile the code that you gave as an example, and the error above is the
result. Here is the entire thing, which is just your code plus two curly
braces.

template<class element_type>
void ToString<std::vector<element_type>>(std::string &s,
std::vector<element_type> &value)
{
}
 
Y

Your Name

Pete Becker said:
That code snippet can't possibly compile as is. It uses a namespace
that hasn't been defined, a template that hasn't been defined, and a
class that hasn't been defined. All those were missing in your
original code, as well.


There is no class or namespace, it's just a global function. Those are
legal in C++.

Anyway, I figured out what was wrong. The correct partial specialization
is this:

template<typename element_type>
void ToString(std::string &s, std::vector<element_type> &value)
{
// Call ToString<element_type> from a loop
}

The additional "std::string<...>" in the original code was incorrect. The
code above compiles fine. I'll know if it actually works soon...
 
Y

Your Name

Pete Becker said:
std is the name of a namespace. vector is a template in that namespace,
and string is a class in that namespace.



"std" is a library that I wrote. It stands for "solar time distortion." I
use it for various astronomy functions.

"vector" refers to the American supercar from the early 1990s:
http://en.wikipedia.org/wiki/Vector_Motors

And "string" is another class that I created, mainly for working with
physics and String Theory calculations.
 

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,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top