endl like behavior

Discussion in 'C++' started by schwehr@gmail.com, Jun 13, 2006.

  1. Guest

    Hi All,

    I am trying to creat a class that that acts much like cerr and am
    having trouble figuring out if it is possible to use std::end with my
    class to designate then end of a group of items. I'd like this class
    to look as close to simple cerr usage as possible to make it easy to
    use. Appologies if this is a dumb question :) Is there a relatively
    simple way to do this? I am not sure what exactly is going on in the
    templates in the ostream header.

    Thanks!
    -kurt

    Something along these lines... not exactly what I want but it
    illustrates the point. slog will not be a stream.

    class slog {
    public:
    slog() {};
    }; // end slog class

    slog& operator<< (slog &s, const int &r) {
    std::cerr << "Hello: " << r << std::endl;
    return s;
    }

    // some sort of endl definition here?

    int main(int argc, char *argv[]) {
    slog log;
    log << 1;
    log << 2;
    log << endl;
    return (EXIT_SUCCESS);
    }
    , Jun 13, 2006
    #1
    1. Advertising

  2. wrote:
    > I am trying to creat a class that that acts much like cerr and am
    > having trouble figuring out if it is possible to use std::end with my
    > class to designate then end of a group of items. I'd like this class
    > to look as close to simple cerr usage as possible to make it easy to
    > use. Appologies if this is a dumb question :) Is there a relatively
    > simple way to do this? I am not sure what exactly is going on in the
    > templates in the ostream header.
    >
    > Thanks!
    > -kurt
    >
    > Something along these lines... not exactly what I want but it
    > illustrates the point. slog will not be a stream.
    >
    > class slog {
    > public:
    > slog() {};
    > }; // end slog class
    >
    > slog& operator<< (slog &s, const int &r) {
    > std::cerr << "Hello: " << r << std::endl;
    > return s;
    > }
    >
    > // some sort of endl definition here?


    Have you tried looking up the declaration/definition of 'endl' in your
    compiler-library headers? It's not that mysterious once you actually
    take a look. Are you game? Try figuring it out yourself with that hint
    and then come back and ask more questions.

    > int main(int argc, char *argv[]) {
    > slog log;
    > log << 1;
    > log << 2;
    > log << endl;
    > return (EXIT_SUCCESS);
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 13, 2006
    #2
    1. Advertising

  3. mlimber Guest

    wrote:
    > Hi All,
    >
    > I am trying to creat a class that that acts much like cerr and am
    > having trouble figuring out if it is possible to use std::end with my
    > class to designate then end of a group of items.


    I presume you mean std::endl, as in your subject line. Here are two
    examples of how to make an iomanip:

    // Output carriage return and line feed
    inline std::eek:stream& crlf( std::eek:stream &os )
    {
    return os << "\r\n";
    }


    // Output a number to a stream in hexidecimal format
    // without altering the state of the stream.
    class Hex
    {
    typedef unsigned ui32;
    typedef int i32;
    std::eek:stringstream m_oss;
    public:
    Hex( const ui32 &i, const i32 width=8 )
    {
    m_oss << "0x" << std::hex << std::setfill('0')
    << std::setw(width) << i;
    }

    Hex( const Hex &h )
    {
    m_oss.str( h.m_oss.str() );
    }

    friend std::eek:stream& operator<<( std::eek:stream &os, const Hex &h )
    {
    return os << h.m_oss.str();
    }
    };

    They can be used like this:

    std::eek:stringstream os;
    os << "The number is" << Hex( 1789 ) << crlf;

    > I'd like this class
    > to look as close to simple cerr usage as possible to make it easy to
    > use. Appologies if this is a dumb question :) Is there a relatively
    > simple way to do this?


    Yes. Use (or contain) a std::eek:stringstream or std::eek:fstream object or
    have your class inherit from std::eek:stream. The latter option should not
    generally be preferred.

    > I am not sure what exactly is going on in the
    > templates in the ostream header.


    That is why you should generally use the facilities provided by the
    library rather than trying to reinvent the wheel.

    Cheers! --M
    mlimber, Jun 13, 2006
    #3
  4. Guest

    Thanks for the quick replies. I am trying to avoid the whole stream
    friend/inheritance since I may be sending messages to a database and/or
    an xml library (or some other equally different sink) or cause the
    compiler to completely eat the class for speed. I did take a look at
    the gcc 4.0 dfinition of endl and found this:

    template<typename _CharT, typename _Traits>
    basic_ostream<_CharT, _Traits>&
    endl(basic_ostream<_CharT, _Traits>& __os)
    { return flush(__os.put(__os.widen('\n'))); }


    I also did the see what the compiler does...

    #include <iostream>
    using namespace std;
    void foo(){
    cout << endl;
    }

    nm foo.o | grep endl
    U __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_

    echo "__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_" |
    c++filt

    std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
    std::char_traits<char> >(std::basic_ostream<char,
    std::char_traits<char> >&)
    , Jun 13, 2006
    #4
  5. Guest

    I also tried creating a function like this...

    namespace std {
    slog& endl(slog& s) {
    std::cerr << "endl called" << std::endl;
    return s;
    }
    } // std

    The compiler then complains with this which says that I am clearly
    missing something :)

    make && ./slogcxx-test
    g++ -o slogcxx-test slogcxx.cpp -Wall -g -DTESTING
    slogcxx.cpp: In function 'int main(int, char**)':
    slogcxx.cpp:36: error: no match for 'operator<<' in 'log << std::endl'
    slogcxx.cpp:23: note: candidates are: slog& operator<<(slog&, const
    int&)
    make: *** [slogcxx-test] Error 1
    , Jun 13, 2006
    #5
  6. mlimber Guest

    wrote:
    > I also tried creating a function like this...
    >
    > namespace std {
    > slog& endl(slog& s) {
    > std::cerr << "endl called" << std::endl;
    > return s;
    > }
    > } // std
    >
    > The compiler then complains with this which says that I am clearly
    > missing something :)
    >
    > make && ./slogcxx-test
    > g++ -o slogcxx-test slogcxx.cpp -Wall -g -DTESTING
    > slogcxx.cpp: In function 'int main(int, char**)':
    > slogcxx.cpp:36: error: no match for 'operator<<' in 'log << std::endl'
    > slogcxx.cpp:23: note: candidates are: slog& operator<<(slog&, const
    > int&)
    > make: *** [slogcxx-test] Error 1


    First, please quote the message you are replying to. It helps everyone
    to follow the conversation.

    Second, you may not add things to namespace std.

    Third, what you're missing is an operator that will use the iomanip.
    Here's what you're looking for:

    #include <iostream>
    using namespace std;

    class slog {}; // end slog class

    slog& operator<< (slog &s, const int r)
    {
    cerr << r;
    return s;
    }

    // Here's the key:
    slog& operator<< ( slog &s, slog&(*iomanip)(slog&) )
    {
    iomanip( s );
    return s;
    }

    slog& endl( slog &s )
    {
    cerr << endl;
    return s;
    }

    int main()
    {
    slog log;
    log << 1 << endl;
    }

    Cheers! --M
    mlimber, Jun 13, 2006
    #6
  7. Tom Widmer Guest

    wrote:
    > Hi All,
    >
    > I am trying to creat a class that that acts much like cerr and am
    > having trouble figuring out if it is possible to use std::end with my
    > class to designate then end of a group of items. I'd like this class
    > to look as close to simple cerr usage as possible to make it easy to
    > use. Appologies if this is a dumb question :) Is there a relatively
    > simple way to do this? I am not sure what exactly is going on in the
    > templates in the ostream header.
    >
    > Thanks!
    > -kurt
    >
    > Something along these lines... not exactly what I want but it
    > illustrates the point. slog will not be a stream.
    >
    > class slog {
    > public:
    > slog() {};
    > }; // end slog class
    >
    > slog& operator<< (slog &s, const int &r) {
    > std::cerr << "Hello: " << r << std::endl;
    > return s;
    > }
    >
    > // some sort of endl definition here?
    >
    > int main(int argc, char *argv[]) {
    > slog log;
    > log << 1;
    > log << 2;
    > log << endl;
    > return (EXIT_SUCCESS);
    > }


    At a very basic level, you could do:

    char const endl = '\n';

    If you need some kind of flushing behaviour, then you need something
    like this:

    //operator for function pointers
    slog& operator<< (slog& log, slog&(*func)(slog&))
    {
    return func(log);
    }

    slog& endl(slog& log)
    {
    log << '\n';
    log.flush();
    return log;
    }

    Tom
    Tom Widmer, Jun 13, 2006
    #7
  8. Guest

    Thanks!!
    -kurt

    mlimber wrote:
    > Second, you may not add things to namespace std.


    Good point!

    > Third, what you're missing is an operator that will use the iomanip.
    > Here's what you're looking for:


    Works like a champ!

    > #include <iostream>
    > using namespace std;
    >
    > class slog {}; // end slog class
    >
    > slog& operator<< (slog &s, const int r)
    > {
    > cerr << r;
    > return s;
    > }
    >
    > // Here's the key:
    > slog& operator<< ( slog &s, slog&(*iomanip)(slog&) )
    > {
    > iomanip( s );
    > return s;
    > }
    >
    > slog& endl( slog &s )
    > {
    > cerr << endl;
    > return s;
    > }
    >
    > int main()
    > {
    > slog log;
    > log << 1 << endl;
    > }
    >
    > Cheers! --M
    , Jun 13, 2006
    #8
  9. mlimber Guest

    mlimber wrote:
    > // Here's the key:
    > slog& operator<< ( slog &s, slog&(*iomanip)(slog&) )
    > {
    > iomanip( s );
    > return s;
    > }


    It will usually not matter unless something funky is going on, but that
    function body should read:

    return iomanip( s );

    Cheers! --M
    mlimber, Jun 13, 2006
    #9
    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. Ensoul Chee
    Replies:
    7
    Views:
    667
    Ensoul Chee
    Sep 9, 2003
  2. Victor Irzak

    Type of endl, ends, flush in g++-3

    Victor Irzak, Sep 10, 2003, in forum: C++
    Replies:
    1
    Views:
    633
    tom_usenet
    Sep 10, 2003
  3. Penna Elabi

    [C++] Arrays and endl

    Penna Elabi, Nov 24, 2003, in forum: C++
    Replies:
    7
    Views:
    535
    Penna Elabi
    Nov 25, 2003
  4. Xav

    std::endl type unknown

    Xav, Dec 12, 2003, in forum: C++
    Replies:
    2
    Views:
    2,951
    Alf P. Steinbach
    Dec 12, 2003
  5. Michael Mellor

    std::endl

    Michael Mellor, Jan 25, 2004, in forum: C++
    Replies:
    13
    Views:
    19,231
Loading...

Share This Page