Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > endl like behavior

Reply
Thread Tools

endl like behavior

 
 
schwehr@gmail.com
Guest
Posts: n/a
 
      06-13-2006
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);
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-13-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      06-13-2006
(E-Mail Removed) 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:stream& crlf( std: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: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:stream& operator<<( std:stream &os, const Hex &h )
{
return os << h.m_oss.str();
}
};

They can be used like this:

std: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:stringstream or std:fstream object or
have your class inherit from std: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

 
Reply With Quote
 
schwehr@gmail.com
Guest
Posts: n/a
 
      06-13-2006
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_ostreamI T_T0_ES6_

echo "__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostream IT_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> >&)

 
Reply With Quote
 
schwehr@gmail.com
Guest
Posts: n/a
 
      06-13-2006
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

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      06-13-2006
(E-Mail Removed) 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

 
Reply With Quote
 
Tom Widmer
Guest
Posts: n/a
 
      06-13-2006
(E-Mail Removed) 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
 
Reply With Quote
 
schwehr@gmail.com
Guest
Posts: n/a
 
      06-13-2006
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


 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      06-13-2006
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

 
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
std::endl Michael Mellor C++ 13 03-23-2008 07:22 PM
std::endl type unknown Xav C++ 2 12-12-2003 09:54 PM
[C++] Arrays and endl Penna Elabi C++ 7 11-25-2003 09:01 AM
Type of endl, ends, flush in g++-3 Victor Irzak C++ 1 09-10-2003 02:24 PM
compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;" Ensoul Chee C++ 7 09-09-2003 02:12 AM



Advertisments