Re: void * instead of bool

From: Alf P. Steinbach (alfps_at_start.no)
Date: 06/21/04


Date: Mon, 21 Jun 2004 18:09:47 GMT


* Karl Heinz Buchegger:
> Marcin Kalicinski wrote:
> >
> > Hi,
> >
> > Why is there void* conversion to check for std::istream failure bit? Why not
> > a conversion to bool?
>
> Because you definitily don't want this to compile:
>
> ifstream if;
> int j;
>
> j = 2 + if;

Assuming you meant to use a non-keyword instead of "if", _why_ on or off
Earth would I definitily (?) not want that to compile?

The same goes for Andrew's example.

On the contrary, a good design does not needlessly limit the user to
do only things the designer is able to foresee the usefulness of.

I think there are two more realistic explanations.

One is that perhaps the original design dates from a time before 'bool'
was introduced in the language. Another is that perhaps there is some
subtle issue with e.g. function overload resolution, e.g. passing an
ifstream object to a constructor where one overload takes a bool. In
the latter case (which does not exclude the first) the conversion to
void* is already one conversion, and void* is a very unspecific type, so
a direct conversion to a more sensible type would, ideally, be chosen.

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


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: 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: boolean usage
    ... "An object declared as type _Bool is large enough to store the ... That mearely constrains the values that result from conversion. ... The width includes any padding bits. ... "The precision of an integer type is the number of bits it uses ...
    (comp.lang.c)
  • Re: boolean usage
    ... Peter Nilsson writes: ... "An object declared as type _Bool is large enough to store the ... That mearely constrains the values that result from conversion. ... 6.5.4p4 "A cast that specifies no conversion has no effect ...
    (comp.lang.c)
  • Re: void * instead of bool
    ... > If you have a conversion to bool then the following nonsense would ... There is a lot of nonsense that does ... compile, but isn't a problem, because nobody writes that nonsense ...
    (comp.lang.cpp)