Re: void * instead of bool

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 06/21/04


Date: Mon, 21 Jun 2004 16:07:26 +0100


"Marcin Kalicinski" <kalita@poczta.onet.pl> wrote in message
news:cb6t4d$p0l$1@korweta.task.gda.pl...
> Hi,
>
> Why is there void* conversion to check for std::istream failure bit? Why
not
> a conversion to bool?

If you have a conversion to bool then the following nonsense would compile

float f = cin;

Conversion to void* is less likely to be called accidentally.

>
> When I try to return an istream from a function that actually returns
bool,
> I get the following warning (in VC .NET):
>
> warning C4800: 'void *' : forcing value to bool 'true' or 'false'
> (performance warning)
>
> I can get rid of it by returning (stream != 0) or (!!stream), but it looks
> ugly to me. Besides that, I suspect that it might perhaps be generating
> unoptimal code. Compiler does not know that void* value holds only 0 or 1
in
> the case of streams, and adds extra code to prevent the value going out of
> bool range. At least that's what the warning is trying to say.

You could do this

    return some_stream.good();

>
> Is there any reason to avoid automatic conversion to bool and prever
> conversion to void* instead?
>

See above.

> Marcin
>
>

john



Relevant Pages

  • Re: I Need an IsNumeric Method
    ... public bool IsNumeric ... //Try a double conversion. ... try to convert and catch the exception ... > o Don't forget that .5 is generally considered okay, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: void * instead of bool
    ... > (performance warning) ... > bool range. ... > Is there any reason to avoid automatic conversion to bool and prever ... the reason is because the Standard says so. ...
    (comp.lang.cpp)
  • Re: Testing for nullptr for a ref object
    ... I'm must admit I haven't read much of the C++/CLI standard. ... my interpretation is that the compiler will consider a conversion ... from x to bool better than the check against nullptr. ... So if a conversion from x to bool exists it will be chosen over the ...
    (microsoft.public.dotnet.languages.vc)
  • Re: bool or BOOL in MFC projects
    ... Conversion of pointer to bool is an accepted feature of the C++ ... while (cin>> i) ... (std::ios has conversion to void*, ...
    (microsoft.public.vc.mfc)
  • Re: void * instead of bool
    ... Earth would I definitily not want that to compile? ... One is that perhaps the original design dates from a time before 'bool' ... ifstream object to a constructor where one overload takes a bool. ... the latter case the conversion to ...
    (comp.lang.cpp)