C++ Logo

std-proposals

Advanced search

Re: [std-proposals] mandatory compiler diagnostic for converting function to boolean

From: connor horman <chorman64_at_[hidden]>
Date: Fri, 8 Sep 2023 19:21:53 -0400
Function pointers contextually convert to bool because they can be null.
But a Function lvalues decay to non-null function pointers (unless you're
using `__attribute__((weak))`).
This also applies to array to pointer conversions so perhaps they should be
included.

Perhaps the rule could be
> A prvalue of arithmetic, unscoped enumeration, pointer, or
pointer-to-member type can be converted to a prvalue 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. *If the expression is
a glvalue of a function or array type, converted to a function pointer or
element pointer, the conversion is deprecated.*

On Fri, 8 Sept 2023 at 16:47, Frederick Virchanza Gotham via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Today I discovered the following bug:
>
> if ( wxIsMainThread ) DoSomething();
>
> The bug here is that 'wxIsMainThread' is a function, and so it should have
> been:
>
> if ( wxIsMainThread() ) DoSomething();
>
> I think where we have a function converting to bool -- but not where
> we have a function pointer converting to bool -- the compiler should
> be mandated to issue a diagnostic.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-09-08 23:22:07