Does std::stringstream know templates

Discussion in 'C++' started by Rene Ivon Shamberger, Dec 5, 2012.

  1. 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
    Rene Ivon Shamberger, Dec 5, 2012
    #1
    1. Advertising

  2. Rene Ivon Shamberger

    Ian Collins Guest

    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
    Ian Collins, Dec 5, 2012
    #2
    1. Advertising

  3. Rene Ivon Shamberger

    Nobody Guest

    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.
    Nobody, Dec 5, 2012
    #3
  4. Rene Ivon Shamberger

    Rui Maciel Guest

    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
    Rui Maciel, Dec 5, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giampiero Gabbiani

    std:stringstream reset

    Giampiero Gabbiani, Oct 19, 2003, in forum: C++
    Replies:
    6
    Views:
    25,283
    Giampiero Gabbiani
    Oct 19, 2003
  2. KidLogik
    Replies:
    2
    Views:
    7,859
    Alberto Barbati
    Feb 24, 2004
  3. JKop
    Replies:
    3
    Views:
    450
  4. recover
    Replies:
    2
    Views:
    783
    recover
    Jul 25, 2006
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    219
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page