Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Does std::stringstream know templates

Reply
Thread Tools

Does std::stringstream know templates

 
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      12-05-2012
Does std::stringstream work with templates, say for instance

void someMethod(int i){
std::string s;
std::stringstream out;
out << i;
s = out.str();
....
}
could be written like this:
template< typename T>
void someMethod(const T& data){
std::string s;
std::stringstream out;
out << data; //<<-- here is the majic would-be
s = out.str();
....
}
However, it seems that std::stringstream does not know templates, or does it. Maybe I am writing the code wrong! Any body?


TIA

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-05-2012
Rene Ivon Shamberger wrote:
> Does std::stringstream work with templates, say for instance
>
> void someMethod(int i){
> std::string s;
> std::stringstream out;
> out << i;
> s = out.str();
> ....
> }
> could be written like this:
> template< typename T>
> void someMethod(const T& data){
> std::string s;
> std::stringstream out;
> out << data; //<<-- here is the majic would-be
> s = out.str();
> ....
> }
> However, it seems that std::stringstream does not know templates, or does it. Maybe I am writing the code wrong! Any body?


Provided T has an appropriate stream operator, this will work.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      12-05-2012
On Tue, 04 Dec 2012 18:33:37 -0800, Rene Ivon Shamberger wrote:

> However, it seems that std::stringstream does not know templates, or
> does it. Maybe I am writing the code wrong! Any body?


Anything which works in non-template code will work in a template,
provided that the template can be instantiated.

Your example will work fine for any type for which an appropriate
operator<< exists (either as method or a function). I've used a
similar template on many occasions.

If your code doesn't work for some particular case, it's probably because
no suitable operator<< can be found.

 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      12-05-2012
Rene Ivon Shamberger wrote:

> Does std::stringstream work with templates, say for instance
>
> void someMethod(int i){
> std::string s;
> std::stringstream out;
> out << i;
> s = out.str();
> ....
> }
> could be written like this:
> template< typename T>
> void someMethod(const T& data){
> std::string s;
> std::stringstream out;
> out << data; //<<-- here is the majic would-be
> s = out.str();
> ....
> }
> However, it seems that std::stringstream does not know templates, or does
> it. Maybe I am writing the code wrong! Any body?


The std::stringstream class works fine.

What did you expected your code would do? And can you provide an example
that reproduces the problem you've been experiencing?


Rui Maciel
 
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
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
LOOK! i just want to know does anyone know... Simon Egginton Javascript 3 07-26-2004 08:11 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM



Advertisments