Automating Type Conversions with stringstream Objects (cont'd)

Templatizing stringstream-based Conversion
In the previous 10-Minute Solution I showed how to abstract generic operations with templates. Since stringstream-based conversion uses the same interface regardless of the actual type being converted, implementing generic conversion functions is easy.

Converting an Arbitrary Type to String
Our first function template takes a value of an arbitrary type T, converts it to string, and writes the result to a user-supplied string called val. We use the stringstream::str() member function to obtain a copy of the stream's internal string:


#include <sstream>
#include <string>
using namespace std;
template <class T>
void string_fmt(string & val, const T & t)
{
 ostringstream oss; // create a stream
 oss << t; // insert value to stream 
 val=oss.str(); // extract string and copy 
}

In the following example, we use the string_fmt() function template to convert the value 10.76 to string and write the result to val:


string val;
string_fmt(val,10.76);

Converting from One Arbitrary Type to Another
Our second function template is called cast_stream(). It converts a variable of type in_value to a variable of type out_type (in_value and out_value are template parameters):


template <class out_type, class in_value>
out_type cast_stream(const in_value & t)
{
 stringstream ss;
 ss << t; // first insert value to stream
 out_type result; // value will be converted to out_type
 ss >> result; // write value to result
 return result;
}

In the following example, cast_stream() converts a string with the value "18.67" to double using cast_stream():


string s="18.67";
double d=cast_stream < double > (s); // assign 18.67 to d

Danny Kalev is a system analyst and software engineer with 12 years of experience, specializing in C++ and object-oriented analysis and design. He is a member of the ANSI C++ standardization committee and the author of ANSI/ISO C++ Professional Programmer's Handbook (Que, 1999, ISBN: 0789720221); check out the review. He can be reached at dannykk@bezeqint.net.


Previous: Inserting a Value to a Stream
 

Introduction
Inserting a Value to a Stream
Templatizing stringstream-based Conversion

Return to Get Help with C/C++ Page

Return to Main Get Help Page


 




Implementing an abstract operation as an ordinary function forces you to define multiple instances of that function, thereby incurring considerable maintenance and debugging overheads.



Use stringstream objects to perform easy, safe, automatic, and object-oriented type conversions.


Find Out More
Eskimo.com FAQs

• "Optimize Abstract Operations with Function Templates"

Tech Tip: Prefer Stringstream Objects to Strstream Objects

Tech Tip: Avoiding Buffer Overflows




TALK BACK
During its standardization process, C++ deprecated several of its libraries and introduced newer libraries that superseded them. Thus, <iostream> superseded <iostream.h>, <fstream> superseded <fstream.h> and so on. Do you still use the old libraries in your code? In your opinion, were these changes really necessary and were they worth the trouble?


Sponsored Links