Need help with my delimited stream wrapper class

Discussion in 'C++' started by rep_movsd, Aug 17, 2011.

  1. rep_movsd

    rep_movsd Guest

    I have a utility class that lets you do something like this :

    TDelimStream<char> ds(cerr, ", ");
    ds << 1 << 2 << 3 << 4;

    prints :
    1, 2, 3, 4

    Now I wish I could have it put an std::endl automatically, I have a
    hunch there's no way to do this.... There seem to be no suitable
    operators that have less precedence than <<

    Any ideas?

    Here is the implementation :

    template<typename ELEM = char>
    struct TDelimStream
    {
    typedef std::basic_ostream<ELEM, std::char_traits<ELEM> > ostream;
    ostream &m_stream;
    string m_sDelim;
    TUntil<1> m_once;

    TDelimStream(ostream &stream, const string& sDelim) :
    m_stream(stream), m_sDelim(sDelim)
    {
    }
    //////////////////////////////////////////////////////////////////////////
    };

    template<typename T, typename T2>
    TDelimStream<T> &operator<<(TDelimStream<T> &p, const T2& val)
    {
    if(p.m_once.occurred())
    {
    p.m_stream << p.m_sDelim;
    }
    p.m_stream << val;
    return p;
    }
    //////////////////////////////////////////////////////////////////////////
    rep_movsd, Aug 17, 2011
    #1
    1. Advertising

  2. On 8/17/2011 1:14 PM, rep_movsd wrote:
    > I have a utility class that lets you do something like this :
    >
    > TDelimStream<char> ds(cerr, ", ");
    > ds<< 1<< 2<< 3<< 4;
    >
    > prints :
    > 1, 2, 3, 4
    >
    > Now I wish I could have it put an std::endl automatically, I have a
    > hunch there's no way to do this.... There seem to be no suitable
    > operators that have less precedence than<<
    >
    > Any ideas?


    How about instead of TDelimStream& your operator<< creates a temporary
    object of some proxy class that also has operator<< which simply
    forwards it to the same stream, and returns itself? Then the destructor
    of that proxy object could forward the endl to the stream just before
    going out of existence...

    >
    > Here is the implementation :
    >
    > template<typename ELEM = char>
    > struct TDelimStream
    > {
    > typedef std::basic_ostream<ELEM, std::char_traits<ELEM> > ostream;
    > ostream&m_stream;
    > string m_sDelim;
    > TUntil<1> m_once;
    >
    > TDelimStream(ostream&stream, const string& sDelim) :
    > m_stream(stream), m_sDelim(sDelim)
    > {
    > }
    > //////////////////////////////////////////////////////////////////////////
    > };
    >
    > template<typename T, typename T2>
    > TDelimStream<T> &operator<<(TDelimStream<T> &p, const T2& val)
    > {
    > if(p.m_once.occurred())
    > {
    > p.m_stream<< p.m_sDelim;
    > }
    > p.m_stream<< val;
    > return p;
    > }
    > //////////////////////////////////////////////////////////////////////////
    >
    >


    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 17, 2011
    #2
    1. Advertising

  3. rep_movsd

    rep_movsd Guest

    On Aug 17, 10:35 pm, Victor Bazarov <> wrote:

    > How about instead of TDelimStream& your operator<< creates a temporary
    > object of some proxy class that also has operator<< which simply
    > forwards it to the same stream, and returns itself?  Then the destructor
    > of that proxy object could forward the endl to the stream just before
    > going out of existence...
    >


    Thanks Victor, your suggestion made me remember temporaries with short
    lives!

    The solution was simple - I just added :

    ~TDelimStream()
    {
    cerr << endl;
    }

    and

    inline TDelimStream<char> print()
    {
    return TDelimStream<char>(cerr, ", ");
    }

    and call it like this :


    print() << 1 << 2 << 3 << 4;

    And it works fine!

    Just when you thing something is impossible in C++ there's always a
    way!!!


    Thanks
    Vivek
    rep_movsd, Aug 17, 2011
    #3
    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. Brendan Duffy
    Replies:
    0
    Views:
    4,041
    Brendan Duffy
    Jul 25, 2003
  2. TJS
    Replies:
    2
    Views:
    340
    Michael Pearson
    Feb 6, 2004
  3. LuckyBoy
    Replies:
    17
    Views:
    765
    Gordon Beaton
    Sep 14, 2006
  4. baldyeti

    wanted: stream wrapper

    baldyeti, Nov 17, 2006, in forum: C++
    Replies:
    1
    Views:
    290
    dasjotre
    Nov 17, 2006
  5. RyanL
    Replies:
    6
    Views:
    671
    Paul McGuire
    Aug 28, 2007
Loading...

Share This Page