Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!ulysses!hector!jss From: jss@hector.UUCP (Jerry Schwarz) Newsgroups: comp.lang.c++ Subject: Re: istream conversion operator Keywords: istream conversion operator Message-ID: <11597@ulysses.homer.nj.att.com> Date: 25 May 89 20:03:56 GMT References: <522@atcmpe.atcmp.nl> Sender: netnews@ulysses.homer.nj.att.com Reply-To: jss@hector.UUCP (Jerry Schwarz) Organization: AT&T Bell Laboratories Lines: 32 In article <522@atcmpe.atcmp.nl> leo@atcmp.nl (Leo Willems) considers code that looks like while ( cin >> i) ... The istream& returned by the extraction operator(>>) gets converted to a void* by operator void*::istream. > >My question is: Why does this conversion take place? There is no >assignment involved (i.e. to a void*) from which the type system can decide >which conversion is appropriate. In C++ conversions can arise implicitly in many contexts other than the right hand side of assignment. In particular in "boolean context", if the type of the expression is not one that can be tested against zero (arithmetic or pointer type) then C++ tries to convert it with a user defined conversion operator. If there is more than one such conversion operator that converts to a type that can be tested against 0, then there is an ambiguity. Boolean contexts are (unless I've overlooked something): expression in an if statement expression in a while statement expression in a repeat until statement 2nd expression in a for statement either operand of logical operators (&&, ||) Jerry Schwarz AT&T Bell Labs, Murray Hill