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: void * instead of bool
    ... If you have a conversion to bool then the following nonsense would compile ... Conversion to void* is less likely to be called accidentally. ... > (performance warning) ...
    (comp.lang.cpp)
  • Re: "a < b < c" not the same as "(a < b) && (b < c)"?
    ... >> The type _Bool is an integer type that can hold only two ... >> (Same for conversion.) No other integer type behaves ... An unsigned int bitfield of length one always holds either ...
    (comp.lang.c)
  • Re: "a < b < c" not the same as "(a < b) && (b < c)"?
    ... > the left operand has type _Bool and the right is a pointer. ... > the value stored in the object designated by the left operand. ... > The conversion of pointers to _Bool should probably also be mentioned ...
    (comp.lang.c)

Loading