Help With Templates

Started by
3 comments, last by visitor 13 years, 11 months ago
I've never really used templates and I'm trying to return a template from a function. I tried to write a function to cast an std::string to a generic value and surprisingly enough it compiles, until I try to use the function, then I get a linker error (wtf?). Here is what I have:

//EngineMath.cpp (added static to definition just for clarity)
template <class Type>
static Type EngineMath::stringToValue(const std::string& str) {
    std::stringstream stream;
    stream << str;

    Type result;

    if ((stream >> result).fail()) {
        //throw an exception
    }

    return(result);
}

//Main.cpp
std::string myString = "436.9728";
float x = EngineMath::stringToValue<float>(myString);

Advertisement
Regarding the linker error, look here.

Also, I'm obliged to mention the existence of boost::lexical_cast(), which does what you're trying to do here, but is a little more sophisticated internally (and is also widely used and well tested).

Note however that I'm not suggesting you shouldn't implement this function yourself! This is a somewhat 'classic' example of a function template, and serves as a very good introduction to that topic. I just wanted to mention that boost::lexical_cast() does exist, and should probably be preferred over similar hand-rolled solutions for production code (in the general case, at least).
Thanks. I am aware of boost::lexical cast however I didn't want to bother adding another dependency just for this.

Offtopic but somewhat related: for parsing numeric data stored in a file, what would you suggest as a solution? I've heard it plenty that std::stringstream and boost::lexical_cast are EXTREMELY, EXTREMELY slow.
Quote:Offtopic but somewhat related: for parsing numeric data stored in a file, what would you suggest as a solution? I've heard it plenty that std::stringstream and boost::lexical_cast are EXTREMELY, EXTREMELY slow.
I don't have any good suggestions off the top of my head :-| I'm certainly a proponent of using the SC++L and Boost wherever reasonable and appropriate, but I imagine there might be better (or at least more efficient) tools available for parsing large amounts of text. (I'm not sure how 'slow' std::stringstream is by itself, but I can certainly imagine that performing a large number of lexical_cast's could be somewhat costly. That's just guesswork though.)

I will mention one thing: if you're storing (or are open to storing) your data in XML format, there's at least one XML parsing library that parses the given text in situ for the sake of efficiency. *Googles...* Here it is. I imagine you could find something similar for JSON as well if you looked around a bit.

Anyway, maybe someone else can recommend a good (and fast) general purpose parser.
Quote:
(I'm not sure how 'slow' std::stringstream is by itself, but I can certainly imagine that performing a large number of lexical_cast's could be somewhat costly.


I suppose one could add specializations to use faster conversion methods for built-in types? A look at boost's source shows that this is actually done there (uses streams with user-supplied buffer?).

This topic is closed to new replies.

Advertisement