By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

endl like behavior

P: n/a
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 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
sc*****@gmail.com 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
Jun 13 '06 #2

P: n/a
sc*****@gmail.com 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::ostream& crlf( std::ostream &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::ostringstream 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::ostream& operator<<( std::ostream &os, const Hex &h )
{
return os << h.m_oss.str();
}
};

They can be used like this:

std::ostringstream 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::ostringstream or std::ofstream object or
have your class inherit from std::ostream. 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

Jun 13 '06 #3

P: n/a
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> >&)

Jun 13 '06 #4

P: n/a
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 '06 #5

P: n/a
sc*****@gmail.com 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

Jun 13 '06 #6

P: n/a
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 '06 #7

P: n/a
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

Jun 13 '06 #8

P: n/a
sc*****@gmail.com 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
Jun 13 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.