473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

endl like behavior

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
8 1652
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Victor Irzak | last post by:
Hello, I have a g++-3.3.1 compiler (latest). I can't figure the type of endl, ends, flush. Here is my program. It compiles with VS.NET, Intel C++ compiler, but not with g++. If you can...
7
by: Penna Elabi | last post by:
In the below Linux C++ program, why do I need endl commands to see the numbers in array exampleArray? I mean that if I remove the endl commands, the program doesn't print anything -- why not? ...
12
by: Michael Mellor | last post by:
I see so many posts on this newsgroup where people use std::endl but I can't see why or where they are getting it from. std::endl outputs a new line and flushes the stream's buffer. It is even 15.7...
9
by: dover | last post by:
For the code, outputfile << *p << endl; Someone suggests: Don't use endl here; it is flushing output stream every time. Use plain '\n'. What's the difference of using "endl" and "\n" above?...
4
by: ricardw | last post by:
Hi! I've tried to find an answer to this in the FAQ and elsewhere on the net, without success. I'm fairly sure that the issue must have turned up before and that at least a handful of people may...
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
1
by: Steve Wonderful | last post by:
I was compiling a VC++ application in VS.NET 2003 and got the following error: The following line #1 will not complied and created the following MS compiler error. If I change endl to "\n" s in...
16
by: Jdban101 | last post by:
I am working on my second program ever, so bear with me. When I try to compile it, I get two error messages(sort of 3): In function `int main()': line 6: `endl' undeclared (first use this...
9
by: Xian | last post by:
Is there a right and proper (tm) way to end a line? Thinking about portability and the mess that the 'wrong' line endings can cause. E.g. std::cout << "Hello World!\n"; or std::cout << "Hello...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.