Topic: Dissallow pointer-to-bool conversions


Author: phalpern@truffle.ma.ultranet.com (Pablo Halpern)
Date: 1996/11/22
Raw View
Jason Merrill <jason@cygnus.com> wrote:

>>>>>> Pablo Halpern <phalpern@truffle.ma.ultranet.com> writes:
>
>> The April 1995 DWP states in [conv.bool]:
>>
[ text deleted ]
>
>> As many have noted, this suddenly makes implicit pointer-to-int
>> conversion legal:
>>
[ example deleted ]
>
>Actually, it doesn't.  A conversion sequence can contain pointer->bool or
>bool->int, but not both.  From the Sep 1996 WP:

I could not find this wording in the April 1995 DWP, so it must be new
in the Sep 1996 WP, which I don't have available to me (is it online
somewhere?). It does seem to be a major improvement. Does this fix all
of the gotcha's with pointer-to-bool conversions?

-------------------------------------------------------------
Pablo Halpern                   phalpern@truffle.ultranet.com

I am self-employed. Therefore, my opinions *do* represent
those of my employer.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1996/11/23
Raw View
Pablo Halpern writes:

> Jason Merrill <jason@cygnus.com> wrote:
>> A conversion sequence can contain pointer->bool or
bool-> int, but not both.  From the Sep 1996 WP:

> I could not find this wording in the April 1995 DWP

Look at [over.ics.scs].  At most one implicit conversion of each
category is allowed, and both pointer->bool and bool->int are in the
same category.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: phalpern@truffle.ma.ultranet.com (Pablo Halpern)
Date: 1996/11/13
Raw View
The April 1995 DWP states in [conv.bool]:

  An rvalue of arithmetic, enumeration, pointer, or  pointer  to  member
  type  can  be converted to an rvalue of type bool.  A zero value, null
  pointer value, or null member pointer value is converted to false; any
  other value is converted to true.

As many have noted, this suddenly makes implicit pointer-to-int
conversion legal:

  char *p;
  int i = p; // pointer-to-bool then bool-to-int
             // If p is null, then i will be zero, else i will be one

This really shouldn't be acceptable. The meaning is obscure to say the
least, and is almost certainly not an intended result. Even an explicit
(fuction-style) cast doesn't fix this. Only re-interpret cast restores
he old meaning (for compilers where int is large enough to hold a char*)
and nothing will restore the error message in the absence of a cast.

The only reason I can see for allowing pointer-to-bool conversions is to
permit the following sort of construct.

char *p;
while (p)
  ...

We could eliminate pointer-to-bool conversions and permit the above
simply by saying that if the conditional expression of a while loop, for
loop, do-while loop or if statement is of pointer type (or
pointer-to-member type), then it is evaluated as false if the pointer is
null and false otherwise.

Is anybody from the committee listening? Is this a real problem? Do I
have a real solution?

-------------------------------------------------------------
Pablo Halpern                   phalpern@truffle.ultranet.com

I am self-employed. Therefore, my opinions *do* represent
those of my employer.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Jason Merrill <jason@cygnus.com>
Date: 1996/11/13
Raw View
>>>>> Pablo Halpern <phalpern@truffle.ma.ultranet.com> writes:

> The April 1995 DWP states in [conv.bool]:
>   An rvalue of arithmetic, enumeration, pointer, or  pointer  to  member
>   type  can  be converted to an rvalue of type bool.  A zero value, null
>   pointer value, or null member pointer value is converted to false; any
>   other value is converted to true.

> As many have noted, this suddenly makes implicit pointer-to-int
> conversion legal:

>   char *p;
>   int i = p; // pointer-to-bool then bool-to-int
>              // If p is null, then i will be zero, else i will be one

Actually, it doesn't.  A conversion sequence can contain pointer->bool or
bool->int, but not both.  From the Sep 1996 WP:

  4   Standard conversions                              [conv]

1 Standard conversions are implicit  conversions  defined  for  built-in
  types.  The full set of such conversions is enumerated in this clause.
  A standard conversion sequence is a sequence of  standard  conversions
  in the following order:

  --Zero or one conversion from the following set: lvalue-to-rvalue con-
    version, array-to-pointer conversion, and  function-to-pointer  con-
    version.

  --Zero  or one conversion from the following set: integral promotions,
    floating point promotion, integral conversions, floating point  con-
    versions,   floating-integral   conversions,   pointer  conversions,
    pointer to member conversions, and boolean conversions.

  --Zero or one qualification conversion.

Jason
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]