R
ricardw
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 be able to point me
in the right direction.
Ok, I was trying to debug some code where endl appearantly wasn't
doing it's job, or at least, did not seem to be inserting an '\n' into
the stream. I managed to crystalize the problem area into this snippet:
#include <ostream>
#include <iostream>
#include <fstream>
using namespace std;
#define USE_OWN_ENDL (0)
class test_stream: public ofstream
{
public:
test_stream() {}
};
template <typename T> test_stream &operator<<(test_stream &s, T t)
{
std::cout << t;
return s;
}
#if USE_OWN_ENDL
test_stream &operator<<(test_stream &s, test_stream &(*fp)(test_stream
&))
{
return (*fp)(s);
}
test_stream &endl(test_stream &s)
{
std::cout << endl;
return s;
}
#endif
int main(int argc, char **argv)
{
cout << "Directly from main:\n";
cout << "Hello world" << endl;
cout << "Hello again" << endl;
cout << "Using 'test_stream' class:\n";
test_stream new_stream;
(new_stream << "Hello world") << endl;
(new_stream << "Hello again") << endl;
}
Now, as written (USE_OWN_ENDL is 0), the program does not output any
newlines to the new_stream. If I set USE_OWN_ENDL to 1, my own endl
function is invoked via the overloaded operator<<(test_stream&,
test_stream& (*fp)(test_stream&) call and it all works nicely. While
this is definitely possible, my guts are telling me there's a more
elegant way.
My feeling is that what is going on here is that since I've created my
own stream class, the base class operator<< does not come into play for
manipulators such as endl. Is it possible to define a suitable
operator<< that does this, so that I don't have to define my own endl?
I've tried various approaches that don't compile properly. Or is there
another approach to get this to work?
A related question: is this something that has changed in later years
because of a change in the standards or something? It seems to me that
the code I'm trying to work with must have worked (i.e. endls used as
described would have output '\n') at some time in some environment, and
it is only a couple of years old. So has endl worked in the way
expected because of a broken compiler or header file implementation at
some point?
Using g++ 3.3.5 (on Linux 2.6.8)
[Disclaimer: yes I know '\n' might be prefereble to endl, yes the
test_stream class doesn't do anything useful as it is written; I've
removed everything that's not related to the problem, the real class
adds some (slight) functionality to the stream]
/Ricard
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 be able to point me
in the right direction.
Ok, I was trying to debug some code where endl appearantly wasn't
doing it's job, or at least, did not seem to be inserting an '\n' into
the stream. I managed to crystalize the problem area into this snippet:
#include <ostream>
#include <iostream>
#include <fstream>
using namespace std;
#define USE_OWN_ENDL (0)
class test_stream: public ofstream
{
public:
test_stream() {}
};
template <typename T> test_stream &operator<<(test_stream &s, T t)
{
std::cout << t;
return s;
}
#if USE_OWN_ENDL
test_stream &operator<<(test_stream &s, test_stream &(*fp)(test_stream
&))
{
return (*fp)(s);
}
test_stream &endl(test_stream &s)
{
std::cout << endl;
return s;
}
#endif
int main(int argc, char **argv)
{
cout << "Directly from main:\n";
cout << "Hello world" << endl;
cout << "Hello again" << endl;
cout << "Using 'test_stream' class:\n";
test_stream new_stream;
(new_stream << "Hello world") << endl;
(new_stream << "Hello again") << endl;
}
Now, as written (USE_OWN_ENDL is 0), the program does not output any
newlines to the new_stream. If I set USE_OWN_ENDL to 1, my own endl
function is invoked via the overloaded operator<<(test_stream&,
test_stream& (*fp)(test_stream&) call and it all works nicely. While
this is definitely possible, my guts are telling me there's a more
elegant way.
My feeling is that what is going on here is that since I've created my
own stream class, the base class operator<< does not come into play for
manipulators such as endl. Is it possible to define a suitable
operator<< that does this, so that I don't have to define my own endl?
I've tried various approaches that don't compile properly. Or is there
another approach to get this to work?
A related question: is this something that has changed in later years
because of a change in the standards or something? It seems to me that
the code I'm trying to work with must have worked (i.e. endls used as
described would have output '\n') at some time in some environment, and
it is only a couple of years old. So has endl worked in the way
expected because of a broken compiler or header file implementation at
some point?
Using g++ 3.3.5 (on Linux 2.6.8)
[Disclaimer: yes I know '\n' might be prefereble to endl, yes the
test_stream class doesn't do anything useful as it is written; I've
removed everything that's not related to the problem, the real class
adds some (slight) functionality to the stream]
/Ricard