[comp.lang.c++] Another cfront 2.0 enum handling bug

graham@cabernet.newcastle.ac.uk (Graham D. Parrington) (12/01/89)

Here's another funny in cfront 2.0's handling of enum types. Consider
the following little program:

#include <iostream.h>

enum en1 { val1, val2 };

inline ostream& operator<< ( ostream& s, en1 v )
{
	s << ( v == val1 ? "val1" : "val2") << "\n";
	return s;
}

main ()
{
	cout << val1 << "\n";
}

When compiled and executed this produces the output:
 0

Thats the value of the enum as an int - the inline operator for the
type is not used. However if the line with cout in it is replaced by

	cout << "" << val1 << "\n";

Then the output is the expected:
 val1

What's going on here? It's really puzzling

--
Graham Parrington, Computing Laboratory, University of Newcastle upon Tyne
ARPA  = Graham.Parrington%newcastle.ac.uk@nsfnet-relay.ac.uk
UUCP  = ...!ukc!newcastle.ac.uk!Graham.Parrington
PHONE = +44 91 222 8067

ark@alice.UUCP (Andrew Koenig) (12/02/89)

In article <1989Dec1.153551.28059@newcastle.ac.uk>, graham@cabernet.newcastle.ac.uk (Graham D. Parrington) writes:

> 	cout << val1 << "\n";

> When compiled and executed this produces the output:
>  0

> However if the line with cout in it is replaced by

> 	cout << "" << val1 << "\n";

> Then the output is the expected:
>  val1

> What's going on here? It's really puzzling

It's the combination of a cfront bug and the fact that cout is not
of type `ostream.'

Rather, cout is of type `ostream_withassign,' which is derived
from `ostream.'

The << operator, of course, returns an ostream.  Thus if you say

	((ostream&) cout) << val1 << "\n";

you'll get the right answer.

Of course,

	cout << ""

already returns an ostream so you needn't worry.

I imagine the bug will be fixed in 2.1
-- 
				--Andrew Koenig
				  ark@europa.att.com