C++ Logo

std-proposals

Advanced search

Re: [std-proposals] comparison between pointers and bool

From: Alejandro Colomar <une+cxx_std-proposals_at_[hidden]>
Date: Mon, 20 Apr 2026 15:31:23 +0200
Hi,

On 2026-04-20T15:52:26+0300, sasho648 wrote:
> Not sure if you have the entire conversation since this is an old thread.
> But the previous example I had was:
>
>
>
> I agree I have had bug where I was doing:
>
> void set_property(const char *name, const char *default_value);
> void set_property_bool(const char*name, bool default_value);
>
> And so I was calling the first with:
>
> set_property("prop", "true")
>
> But then when I call the second version with
>
> set_property_bool("prop", "true")
>
> It compiles fine and even works as expected until I changed it to
>
> set_property_bool("prop", "false")
>
> And then I banged my head for a while.

Thanks!

>
> I also agree this was probably a bad design also but still.
>
> Which didn't have default parameters.
>
> And the topic is - should converson from pointer to bool like this be
> allowed.

Definitely yes, it should be allowed.

You can request your compiler or static analyzer a voluntary diagnostic
with some implementation-defined warning flag. But there are many
places where conversion from pointer to bool is wanted.

In C, you could force the parameter to be of a certain type in this
specific API, with macros:

 alx_at_devuan:~/tmp$ cat g.c | nl -ba
      1 #define set_property_bool(name, val) \
      2 ( \
      3 _Generic(val, bool: (void)0), \
      4 set_property_bool(name, val) \
      5 )
      6
      7 void (set_property_bool)(const char *name, bool val);
      8
      9 int
     10 main(void)
     11 {
     12 set_property_bool("foo", true);
     13 set_property_bool("foo", "true");
     14 }
 alx_at_devuan:~/tmp$ gcc g.c
 g.c: In function ‘main’:
 g.c:13:34: error: ‘_Generic’ selector of type ‘char *’ is not compatible with any association
    13 | set_property_bool("foo", "true");
       | ^~~~~~
 g.c:3:18: note: in definition of macro ‘set_property_bool’
     3 | _Generic(val, bool: (void)0), \
       | ^~~

I ignore how to do that in C++, though.


Cheers,
Alex

-- 
<https://www.alejandro-colomar.es>

Received on 2026-04-20 13:31:45